Date Time methods ASP.NET

ASP.NET Microsoft Technology provides date time with the use of SYSTEM TIME 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(2018,08,15);
                
                Console.WriteLine("Declare Date and Time: {0}", DT);
    
                Console.ReadLine();
            }
        }
    }


    Following will be the output:

    
    Declare Date and Time : 08/15/2018 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(2018,08,15);
                string Long_Date = DT.ToString("D");
    
                Console.WriteLine("Date     : {0}", DT);
                Console.WriteLine("Long Date:  {0}", Long_Date);
                           
                Console.ReadLine();
            }
        }
    }


    
    //Out Put:
    Date :08/15/18 12:00:00 AM
    Long Date : Wednesday,August 15, 2018


    DAY: specify "DD".

    
    using System;
    namespace DateTimeDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime DT = new DateTime(2018,08,15);
                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: 08/15/18 12:00:00 AM
    Day Part Of Date: 15


    Day of Week: use the Enum 'DayOfWeek'

    
    using System;
    namespace DateTimeDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime DT = new DateTime(2018,08,15);
               
                Console.WriteLine("Week of Day:  {0}", DT.DayOfWeek);
                           
                Console.ReadLine();
            }
        }
    }


    
    //Out Put
    Week of Day : Wednesday


    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(2018,08,15);
                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:08/15/18 12:00:00 AM
    End Date of Week:08/22/18 12:00:00 AM
    Next Date of Start Date:08/16/18 12:00:00 AM
    Previous Date of Start Date: 08/14/18 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(2018,08,15);
                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 : 08/15/18 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(2018,08,15);
                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: 08/15/18 12:00:00 AM
    Year last two Number:18
    Year Number:2018


    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(2018, 8, 15, 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:08/8/18 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(2018,08,15, 5, 45, 55);
                DateTime Time_PM = new DateTime(2018, 08, 15, 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: 08/15/18 5:45:55 AM
    AM Time Hour:  05
    
    Date and Time: 08/15/18 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(2018,08,15, 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 :08/15/18 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(2018,08,15, 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:08/15/18 5:45:55 AM
    Second Part in Time:55

    Comments

    Popular posts from this blog

    Email Sending through O365 using OAuth Protocol

    IISRESET vs App Pool Recycling ?

    Deploy .Net6.0 Web api with docker