DateTime Method Using ASP.Net C#

Date and Time are very important builtin methods of ASP.Net Microsoft technology. Microsoft technology represents dates and times in the operating system using SYSTEMTIME method.

You will use date and time values declared with a DateTime variable.

Declare a Datetime variable as in the following:

 

  1. DateTime dt = new DateTime();  

 

You can get a date and time value with a DateTime variable. You can also access the milliseconds, seconds, minutes, hours, days, weeks, months and years using DateTime.

There are mainly the following two parts of a DATETIME:

  1. DATE
  2. TIME

DATE

The date consists of the three parts of day, month and year.

Now, we first see the default system date and time get using the Microsoft built-in system DateTime type.

Example

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime DT = new DateTime();  
  13.   
  14.             Console.WriteLine("Default System Satrting Date and Time: {0}", DT);  
  15.   
  16.             Console.ReadLine();  
  17.         }  
  18.     }  
  19. }  

 

Output

 

Figure 1: Date

The following shows how to declare a date.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime DT = new DateTime(2015,06,27);  
  13.               
  14.             Console.WriteLine("Declare Date and Time: {0}", DT);  
  15.   
  16.             Console.ReadLine();  
  17.         }  
  18.     }  
  19. }  

 

Output

 

Figure 2: Declare Date manually 

The date part consists of the three parts of dd, mm and yyyy.

Now we are getting all the date part one by one with declaring a Date.

Long Date

A Long Date is obtained with a “D”.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime DT = new DateTime(2015,06,27);  
  13.             string Long_Date = DT.ToString("D");  
  14.   
  15.             Console.WriteLine("Date     : {0}", DT);  
  16.             Console.WriteLine("Long Date:  {0}", Long_Date);  
  17.                          
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21. }  

 

Output

 

Figure 3: Long Date

DAY

First we are getting the day part of our date.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime DT = new DateTime(2015,06,27);  
  13.             string Day = DT.ToString("dd");  
  14.               
  15.             Console.WriteLine("Declare Date and Time: {0}", DT);  
  16.             Console.WriteLine("Day Part Of Date: {0}", Day);  
  17.   
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21. }  

 

Output

 

Figure 4: Day

DAY OF WEEK

This example is getting the day name.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime DT = new DateTime(2015,06,27);  
  13.              
  14.             Console.WriteLine("Week of Day:  {0}", DT.DayOfWeek);  
  15.                          
  16.             Console.ReadLine();  
  17.         }  
  18.     }  
  19. }  

 

Output

 

Figure 5: Day of Week

DAY ADD AND SUBTRACT

The AddDay() method is for adding and subtracting days. The AddDay() method is for changing to a future or previous day.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime Start_DT = new DateTime(2015,06,27);  
  13.             Console.WriteLine("Start Date of Week: {0}", Start_DT);  
  14.                         
  15.             DateTime End_DT = Start_DT.AddDays(7);  
  16.             Console.WriteLine("\nEnd Date of Week: {0}", End_DT);  
  17.   
  18.             DateTime Next_DT = Start_DT.AddDays(1);  
  19.             Console.WriteLine("\nNext Date of Start Date:   {0}", Next_DT);  
  20.   
  21.             DateTime Previous_DT = Start_DT.AddDays(-1);  
  22.             Console.WriteLine("\nPrevious Date of Start Date:   {0}", Previous_DT);  
  23.              
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27. }  

 

Output

 

Figure 6: DAY ADD AND SUBTRACT

MONTH

Get a month part of a date. Also the month is displayed in a different format.

Example:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime DT = new DateTime(2015,06,27);  
  13.             string Month_No = DT.ToString("MM");  
  14.             string Month_Chr = DT.ToString("MMM");  
  15.             string Month_Name = DT.ToString("MMMM");  
  16.                           
  17.             Console.WriteLine("Declare Date and Time: {0}", DT);  
  18.             Console.WriteLine("\n------------------------------");  
  19.             Console.WriteLine("Month Number: {0}", Month_No);  
  20.             Console.WriteLine("Month Character: {0}", Month_Chr);  
  21.             Console.WriteLine("Month Name: {0}", Month_Name);  
  22.   
  23.             Console.ReadLine();  
  24.         }  
  25.     }  
  26. }  

 

Output

 

Figure 7: Month

YEAR

Get a year part of a date. Also the year is displayed in a different format with the year digits.

Example:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime DT = new DateTime(2015,06,27);  
  13.             string Year_Last_2No = DT.ToString("yy");  
  14.             string Year = DT.ToString("yyyy");  
  15.                           
  16.             Console.WriteLine("Declare Date and Time: {0}", DT);  
  17.             Console.WriteLine("\n------------------------------");  
  18.             Console.WriteLine("Year last two Number: {0}", Year_Last_2No);  
  19.             Console.WriteLine("Year Number: {0}",Year);  
  20.   
  21.             Console.ReadLine();  
  22.         }  
  23.     }  
  24. }  

 

Output

 

Figure 8: Year

TIME

The time consists of hour, minute and second.

The hour starts with 0.00 and goes to 23.59 and also has a AM or PM.

Example 1: Get Full Time of the Day

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime time = new DateTime(2015, 6, 27, 16, 15, 45);  
  13.   
  14.             Console.WriteLine("Date and Time: {0}\n", time);  
  15.             Console.WriteLine("Time of Day:   {0}\n", time.TimeOfDay);  
  16.              
  17.             Console.ReadLine();  
  18.         }  
  19.     }  
  20. }  

 

Output

 

Figure 9: Time

Example 2: Get Hour Part

The hour is the first part of time as in HH:MM:SS.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime Time_AM = new DateTime(2015,06,27, 5, 45, 55);  
  13.             DateTime Time_PM = new DateTime(2015, 06, 27, 18, 45, 55);  
  14.   
  15.             string Hour_AM = Time_AM.ToString("hh");  
  16.             string Hour_PM = Time_PM.ToString("hh");  
  17.   
  18.             Console.WriteLine("Date and Time: {0}", Time_AM);  
  19.             Console.WriteLine("AM Time Hour:    {0}\n", Hour_AM);  
  20.   
  21.             Console.WriteLine("Date and Time: {0}", Time_PM);  
  22.             Console.WriteLine("PM Time Hour:    {0}\n", Hour_PM);  
  23.   
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27. }  

 

Output

 

Figure 10: Time Get Hour Part

MINUTE

The minute is the middle part of time as in HH:MM:SS.

The minute number is between 00 and 59.

Example:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime Time = new DateTime(2015,06,27, 5, 45, 55);  
  13.              
  14.             string Minute = Time.ToString("mm");  
  15.              
  16.             Console.WriteLine("Date and Time: {0}", Time);  
  17.             Console.WriteLine("Minute Part in Time:{0}", Minute);  
  18.               
  19.             Console.ReadLine();  
  20.         }  
  21.     }  
  22. }  

 

Output

 

Figure 11: Minute

SECOND

The second is the last part of time as in HH:MM:SS.

It is also between 00 and 59 numeric.

Example:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime Time = new DateTime(2015,06,27, 5, 45, 55);  
  13.              
  14.             string Second = Time.ToString("ss");  
  15.              
  16.             Console.WriteLine("Date and Time: {0}", Time);  
  17.             Console.WriteLine("Second Part in Time:{0}", Second);  
  18.               
  19.             Console.ReadLine();  
  20.         }  
  21.     }  
  22. }  

 

Output

 

Figure 12: Second

DATE FORMAT

Now we will see how to declare a date in various formats as in the following example.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DateTimeDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             DateTime DATE = new DateTime(2015,06,27, 5, 45, 55);  
  13.   
  14.             Console.WriteLine("Date and Time:  {0}", DATE);  
  15.   
  16.             Console.WriteLine("\nDate and Time:  {0}", DATE.ToString("dd-MMM-yy"));  
  17.             Console.WriteLine("Date and Time:  {0}", DATE.ToString("MM/dd/yyyy"));  
  18.             Console.WriteLine("Date and Time:  {0}", DATE.ToString("M/d/yyyy"));  
  19.             Console.WriteLine("Date and Time:  {0}", DATE.ToString("MM/dd/yy"));  
  20.             Console.WriteLine("Date and Time:  {0}", DATE.ToString("M/d/yy"));  
  21.             Console.WriteLine("Date and Time:  {0}", DATE.ToString("yyyy-MM-dd"));  
  22.             Console.WriteLine("Date and Time:  {0}", DATE.ToString("yy/MM/dd"));  
  23.                          
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27. }  

 

Output

 

Figure 13: Date Format

I will hope you like this article. The DateTime method is very small but very important in any system application or project.