The System.DateTime Class

The System.DateTime class of the .NET Framework allows you to use, store, and manipulate data of time and date. It has methods for manipulation of dates such as adding or subtracting days, months, or years to the current date and then returning the resulting date. It has some methods to show your date in various formats. The following code demonstrates how you can use the DateTime class to use time and date in your program.

using System;
 
public class Program
{
    public static void Main()
    {
        DateTime newyear = new DateTime(2010, 12, 25);
 
        Console.WriteLine(newyear.ToString());
    }
}
12/25/2010 12:00:00 AM

We used the constructor which accepts the year, the month, and the date to initialize our DateTime object. We used the   ToString ()method of the DateTime class to display the date and time. The default ToString() method displays your DateTime object in this format: MM/DD/YYYY HH:MM:SS AM/PM. You will see that you have different methods to modify the formatting of the output. The DateTimemethod has multiple constructors that you can use. The table below lists some of the most basic ones.

Constructors
DateTime()
DateTime(int year, int month, int day)
DateTime(int year, int month, int day, int hour, int minute, int second)
DateTime(int year, int month, int day, int hour,
int minute, int second, int millisecond)

Using the default parameterless constructor initializes the date to 01/01/0001 12:00:00 AM. To obtain the current date and time, you can use the Now static property of the DateTime class. To just get the current date, you can use the Today property.

DateTime now = DateTime.Now;
DateTime today = DateTime.Today;

To modify how your DateTime object will be presented, you can use several formatting methods available.

Method Sample Output
ToLongDateString() Saturday, Octoboer 2, 2010
ToLongTimeString() 4:14:18 PM
ToShortDateString() 10/2/2010
ToShortTimeString() 4:14 PM

You can even use the String.Format method to have a customized formatting.

Specifier Type Example Output
dd Day {0:dd} 10
ddd Day name {0:ddd} Tue
dddd Full Day Name {0:ddd} Tuesday
f, ff, … Second Fractions {0:fff} 932
gg, … Era {0:gg} A.D.
hh 2 digit hour {0:hh} 10
HH 2 digit hour,
24hr format
{0:HH} 22
mm Minute 00-59 {0:mm} 38
MM Month 01-12 {0:MM} 12
MMM Month abbreviation {0:MMM} Dec
MMMM Full month name {0:MMMM} December
ss Seconds 00-59 {0:ss} 46
tt AM or PM {0:tt} PM
yy Year, 2 digits {0:yy} 02
yyyy Year {0:yyyy} 2002
zz Timezone offset,
2 digits
{0:zz} -05
zzz Full timezone
offset
{0:zzz} -05:00
: Seperator {0:hh:mm:ss} 10:43:20
/ Seperator {0:dd/MM/yyyy} 10/2/2010

For example:

Console.WriteLine("The current date is {0:dd/MM/yyy}", DateTime.Now);
The current date is 10/2/2010

DateTime Methods

The DateTime class provides some methods that deal with date and time data. Some of these methods are listed in the table below.

Method Description
AddDays Adds an amount of time to
the value of the DateTime object.
Accepts a double argument which
indicates the amount of time to add.
Use negative value to subtract time instead.
AddHours
AddMilliseconds
AddMinutes
AddMonths
AddSeconds
AddTicks
AddYears
Equals Checks whether that DateTime object
is equal to the DateTime argument passed to the method.
IsDaylightSavingTime Check whether the instance of the DateTime
is within the Daylight Saving Time range
for the current time zone.
ToUniversalTime Converts the value of the DateTime instance
to Coordinated Universal Time (UTC)
DaysInMonth Static. Returns the number of days of the
specified month and year.
IsLeapYear Static. Tells whether the specified year is
a leap year.

Some of the properties of the DateTime class is listed below.

Property Description
Date Returns the date component.
Day Returns the day of the month.
DayOfWeek Returns the day of the week.
DayOfYear Returns the day of the year.
Hour Returns the hour component.
Millisecond Returns the milliseconds component.
Minute Returns the minute component.
Month Returns the month component.
Second Returns the second component.
TimeOfDay Returns the current time of the day.
Year Returns the year component.
Now Static. Returns the current date and time.
Today Static. Returns the current date only.
UtcNow Static. Returns the current date and time in UTC.

Let’s take a look at another example that demonstrate the use of certain methods and properties of the DateTime class.

using System;
 
public class Program
{
    public static void Main()
    {
        DateTime myDate = DateTime.Now;
 
        Console.WriteLine("Year: " + myDate.Year);
        Console.WriteLine("Month: " + myDate.Month);
        Console.WriteLine("Day: " + myDate.Day);
        Console.WriteLine("Today is {0}.", myDate.DayOfWeek.ToString());
     
        //Assign newDate with the current date added by 3 days
        DateTime newDate = myDate.AddDays(3);
        Console.WriteLine("The date 3 days from now is {0}.", 
                  newDate.ToShortDateString());
 
        //Assign newdate with the current date subtracted by 3 days
        newDate = myDate.AddDays(-3);
        Console.WriteLine("The date 3 days ago is {0}.", newDate.ToShortDateString()); 
    }
}
Year: 2010
Month: 10
Day: 3
Today is Sunday.
The date 3 days from now is 10/6/2010.
The date 3 days ago is 9/30/2010.

We used the properties of the DateTime class to display the separate components of the date. We then demonstrate how we can modify a date by adding and subtracting days to the value of our DateTime object. Notice that we used a negative number when we subtracted 3 days. We also used the ToShortDateString() method to display a short form of the date.