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:
- 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:
- DATE
- 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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- 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();
- }
- }
- }
Output
Figure 1: Date
The following shows how to declare a date.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime DT = new DateTime(2015,06,27);
-
- Console.WriteLine("Declare Date and Time: {0}", DT);
-
- Console.ReadLine();
- }
- }
- }
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”.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime DT = new DateTime(2015,06,27);
- string Long_Date = DT.ToString("D");
-
- Console.WriteLine("Date : {0}", DT);
- Console.WriteLine("Long Date: {0}", Long_Date);
-
- Console.ReadLine();
- }
- }
- }
Output
Figure 3: Long Date
DAY
First we are getting the day part of our date.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime DT = new DateTime(2015,06,27);
- string Day = DT.ToString("dd");
-
- Console.WriteLine("Declare Date and Time: {0}", DT);
- Console.WriteLine("Day Part Of Date: {0}", Day);
-
- Console.ReadLine();
- }
- }
- }
Output
Figure 4: Day
DAY OF WEEK
This example is getting the day name.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime DT = new DateTime(2015,06,27);
-
- Console.WriteLine("Week of Day: {0}", DT.DayOfWeek);
-
- Console.ReadLine();
- }
- }
- }
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime Start_DT = new DateTime(2015,06,27);
- 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();
- }
- }
- }
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime DT = new DateTime(2015,06,27);
- 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();
- }
- }
- }
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime DT = new DateTime(2015,06,27);
- 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();
- }
- }
- }
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime time = new DateTime(2015, 6, 27, 16, 15, 45);
-
- Console.WriteLine("Date and Time: {0}\n", time);
- Console.WriteLine("Time of Day: {0}\n", time.TimeOfDay);
-
- Console.ReadLine();
- }
- }
- }
Output
Figure 9: Time
Example 2: Get Hour Part
The hour is the first part of time as in HH:MM:SS.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime Time_AM = new DateTime(2015,06,27, 5, 45, 55);
- DateTime Time_PM = new DateTime(2015, 06, 27, 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();
- }
- }
- }
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime Time = new DateTime(2015,06,27, 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();
- }
- }
- }
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime Time = new DateTime(2015,06,27, 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();
- }
- }
- }
Output
Figure 12: Second
DATE FORMAT
Now we will see how to declare a date in various formats as in the following example.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace DateTimeDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- DateTime DATE = new DateTime(2015,06,27, 5, 45, 55);
-
- Console.WriteLine("Date and Time: {0}", DATE);
-
- Console.WriteLine("\nDate and Time: {0}", DATE.ToString("dd-MMM-yy"));
- Console.WriteLine("Date and Time: {0}", DATE.ToString("MM/dd/yyyy"));
- Console.WriteLine("Date and Time: {0}", DATE.ToString("M/d/yyyy"));
- Console.WriteLine("Date and Time: {0}", DATE.ToString("MM/dd/yy"));
- Console.WriteLine("Date and Time: {0}", DATE.ToString("M/d/yy"));
- Console.WriteLine("Date and Time: {0}", DATE.ToString("yyyy-MM-dd"));
- Console.WriteLine("Date and Time: {0}", DATE.ToString("yy/MM/dd"));
-
- Console.ReadLine();
- }
- }
- }
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.