Discussing Date and Time Method In ASP.NET C#


In this article we will discuss about Date & Time methods of ASP.NET, we will use C# to write code-snippet. ASP.NET Microsoft Technology provides date time with the use of SYSTEMTIME method of operating system.

Discussing DateTime Method Using Asp.Net C#

Abstract
In this article we will discuss about Date & Time methods of ASP.NET, we will use C# to write code-snippet.

ASP.NET Microsoft Technology provides date time with the use of SYSTEMTIME method of operating system.

Declaring Date Time
We can use date and time value by declaring with DateTime variable,
Do not forget to add 'System' namespace

Refer below code-snippet:


DateTime dt = new DateTime();


Playing with DateTime
We can get Date & Time value using above declared DateTime variable.
We can also get millisecond, second, minute, hour, day, week, month, year.

Parts of DateTime
Followings are the two main parts of DATETIME:

  • DATE

  • TIME


  • Lets discuss these in more details:

    DATE
    'Date' contains three part of day, month and year.

    Take a look into code-snippet mentioned below, where we are getting the default
    System Date and time, using Microsoft built in system DateTime Variable.


    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime DT = new DateTime();

    Console.WriteLine("Default System Satrting Date and Time: {0}", DT);

    Console.ReadLine();
    }
    }
    }


    Following will be the output:


    Default System Satrting Date and Time: 01/01/01 12:00:00 AM


    In following code-snippet we are getting user-specific date.


    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime DT = new DateTime(2015,07,18);

    Console.WriteLine("Declare Date and Time: {0}", DT);

    Console.ReadLine();
    }
    }
    }


    Following will be the output:

    Declare Date and Time : 07/18/2015 12:00:00 AM


    Further, Date Part is having three part dd, mm and yyyy.

    Followings are the various way to get teh date part:

    Long Date:specify "D".


    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime DT = new DateTime(2015,07,18);
    string Long_Date = DT.ToString("D");

    Console.WriteLine("Date : {0}", DT);
    Console.WriteLine("Long Date: {0}", Long_Date);

    Console.ReadLine();
    }
    }
    }



    //Out Put:
    Date :07/18/15 12:00:00 AM
    Long Date : Saturday,July 18, 2015


    DAY: specify "DD".


    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime DT = new DateTime(2015,07,18);
    string Day = DT.ToString("dd");

    Console.WriteLine("Declare Date and Time: {0}", DT);
    Console.WriteLine("Day Part Of Date: {0}", Day);

    Console.ReadLine();
    }
    }
    }



    //Out put
    Declare Date and Time: 07/18/15 12:00:00 AM
    Day Part Of Date: 18


    Day of Week: use the Enum 'DayOfWeek'


    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime DT = new DateTime(2015,07,18);

    Console.WriteLine("Week of Day: {0}", DT.DayOfWeek);

    Console.ReadLine();
    }
    }
    }



    //Out Put
    Week of Day : Saturday


    Date operations:

    AddDay() method is used to add / substract days.

    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime Start_DT = new DateTime(2015,07,18);
    Console.WriteLine("Start Date of Week: {0}", Start_DT);

    DateTime End_DT = Start_DT.AddDays(7);
    Console.WriteLine("\nEnd Date of Week: {0}", End_DT);

    DateTime Next_DT = Start_DT.AddDays(1);
    Console.WriteLine("\nNext Date of Start Date: {0}", Next_DT);

    DateTime Previous_DT = Start_DT.AddDays(-1);
    Console.WriteLine("\nPrevious Date of Start Date: {0}", Previous_DT);

    Console.ReadLine();
    }
    }
    }


    //Out Put
    Start Date of Week:07/18/15 12:00:00 AM
    End Date of Week:07/25/15 12:00:00 AM
    Next Date of Start Date:07/19/15 12:00:00 AM
    Previous Date of Start Date: 07/17/15 12:00:00 AM


    Month value:Get a month part with declaring date.
    Specify:"M" , "MM" or "MMM"


    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime DT = new DateTime(2015,07,18);
    string Month_No = DT.ToString("MM");
    string Month_Chr = DT.ToString("MMM");
    string Month_Name = DT.ToString("MMMM");

    Console.WriteLine("Declare Date and Time: {0}", DT);
    Console.WriteLine("\n------------------------------");
    Console.WriteLine("Month Number: {0}", Month_No);
    Console.WriteLine("Month Character: {0}", Month_Chr);
    Console.WriteLine("Month Name: {0}", Month_Name);

    Console.ReadLine();
    }
    }
    }


    //Out Put
    Declare Date and Time : 07/18/15 12:00:00 AM
    Month Number: 07
    Month Character: Jul
    Month Name:July


    Year value: Get a year part of declared date.
    Specify: "yy" ot "yyy"


    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime DT = new DateTime(2015,07,18);
    string Year_Last_2No = DT.ToString("yy");
    string Year = DT.ToString("yyyy");

    Console.WriteLine("Declare Date and Time: {0}", DT);
    Console.WriteLine("\n------------------------------");
    Console.WriteLine("Year last two Number: {0}", Year_Last_2No);
    Console.WriteLine("Year Number: {0}",Year);

    Console.ReadLine();
    }
    }
    }


    //Out Put
    Declare Date and Time: 07/18/15 12:00:00 AM
    Year last two Number:15
    Year Number:2015


    Discussing Time part:Time part contains hour, minute and second.

    Hour value: ranges between 0.00 to 23.59 (or also AM and PM).

    Following code-snippet is to get full time of day:

    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime time = new DateTime(2015, 7, 18, 16, 15, 45);

    Console.WriteLine("Date and Time: {0}\n", time);
    Console.WriteLine("Time of Day: {0}\n", time.TimeOfDay);

    Console.ReadLine();
    }
    }
    }


    //Out Put:
    Date and Time:07/8/15 4:15:45 PM
    Time of Day: 16:15:45


    Following code-snippet is for Hour:


    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime Time_AM = new DateTime(2015,07,18, 5, 45, 55);
    DateTime Time_PM = new DateTime(2015, 07, 18, 18, 45, 55);

    string Hour_AM = Time_AM.ToString("hh");
    string Hour_PM = Time_PM.ToString("hh");

    Console.WriteLine("Date and Time: {0}", Time_AM);
    Console.WriteLine("AM Time Hour: {0}\n", Hour_AM);

    Console.WriteLine("Date and Time: {0}", Time_PM);
    Console.WriteLine("PM Time Hour: {0}\n", Hour_PM);

    Console.ReadLine();
    }
    }
    }


    //Out Put
    Date and Time: 07/18/15 5:45:55 AM
    AM Time Hour: 05

    Date and Time: 07/18/15 6:45:55 PM
    AM Time Hour: 06


    Minute value:Minute is middle part of time (HH:MM:SS) and tanges between 00 to 59.


    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime Time = new DateTime(2015,07,18, 5, 45, 55);

    string Minute = Time.ToString("mm");

    Console.WriteLine("Date and Time: {0}", Time);
    Console.WriteLine("Minute Part in Time:{0}", Minute);

    Console.ReadLine();
    }
    }
    }


    //Out Put
    Date and Time :07/18/15 5:45:55 AM
    Minute Part in Time:45


    Second value:
    Second is third part of Time (HH:MM:SS) and ranges between 00 to 59.

    using System;
    namespace DateTimeDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    DateTime Time = new DateTime(2015,07,18, 5, 45, 55);

    string Second = Time.ToString("ss");

    Console.WriteLine("Date and Time: {0}", Time);
    Console.WriteLine("Second Part in Time:{0}", Second);

    Console.ReadLine();
    }
    }
    }


    //Out Put:
    Date and Time:07/18/15 5:45:55 AM
    Second Part in Time:55


    Comments

    Author: Debendra Dash17 Apr 2016 Member Level: Bronze   Points : 0

    good one..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: