This tutorial shows you how to use the .NET object DateTime: how to get the current date and time, how to filter the output, check for leap years, find the number of days in a given month, add and subtract time to the current date and time.
Here you’ll see some examples of how to use the DateTime object.
The range of DateTime
DateTime has quite a range. It starts from January 1st, year 1, hour 12:00:00 and ends in year 9999, December 31st at 11:59:59 PM. So you don’t have to worry unless your application won’t be updated until the year 9999 .
Get the current date and time
That’s what the DateTime object is used most of the time.
You can test the following code in a Console application.
Console.WriteLine("Current date and time: {0}", System.DateTime.Now);
Console.WriteLine("Current date: {0}", System.DateTime.Today);
Here’s my output:
Current date and time: 9/18/2004 7:23:09 Current date: 9/18/2004 12:00:00 Press any key to continue |
As expected, using DateTime.Now you’ll get the current date and time of the machine on which the code runs.
Using DateTime.Today you get only the current exact date, but not the time which is always set to 12:00:00.
9/18/2004 6:03:07 uses the American date format system. It’s straightforward that 9 represents the month (September) and 18 is the day.
Outputing current second, minute, hour…
Maybe you want to greet the user depending on the hour so you only need DateTime to provide you with the current hour on the user’s machine.
You can easily retrieve only what interests you (current year, day of year…) with the DateTime object. The following code outputs information provided by DateTime separately:
System.DateTime dt = System.DateTime.Now;
// The number of ticks
Console.WriteLine("Ticks: {0}", dt.Ticks);
// The current year
Console.WriteLine("Year: {0}", dt.Year);
// The day of the year
Console.WriteLine("DayOfYear: {0}", dt.DayOfYear);
// The current month
Console.WriteLine("Month: {0}", dt.Month);
// The day of the week (0 = Sunday)
Console.WriteLine("DayOfWeek: {0}", dt.DayOfWeek);
// The current day
Console.WriteLine("Day: {0}", dt.Day);
// The current hour
Console.WriteLine("Hour: {0}", dt.Hour);
// The current minute
Console.WriteLine("Minute: {0}", dt.Minute);
// The current second
Console.WriteLine("Second: {0}", dt.Second);
// The current millisecond
Console.WriteLine("Millisecond: {0}", dt.Millisecond);
Here’s the output I got:
Ticks: 632311327386856160 Year: 2004 DayOfYear: 262 Month: 9 DayOfWeek: Saturday Day: 18 Hour: 19 Minute: 32 Second: 18 Millisecond: 685 Press any key to continue |
Ticks represent the number of ticks since January 1st, year 1, hour 12:00 (midnight). One tick equals 100 nanoseconds.
Check for leap year
No need to create a function which calculates to see if one year is a leap year because now you have IsLeapYear() which returns true if it is a leap year and false if it isn’t.
Console.WriteLine("Is 2005 a leap year? {0}", System.DateTime.IsLeapYear(2005));
2005 isn’t a leap year so we’ll get the answer False in the console. If you want to see if the current year is a leap year use the following combination:
Console.WriteLine("Is this year a leap year? {0}", System.DateTime.IsLeapYear(System.DateTime.Now.Year));
I’m running this in 2004, and IsLeapYear() returns true because 2004 is indeed a leap year.
How many days does this month have?
You’ll often need to know the number of days the current month has, and since February ‘varies’ this proves useful. Here’s how you obtain the number of days in the current month and current year:
Console.WriteLine("Days in this month: {0}", System.DateTime.DaysInMonth(System.DateTime.Now.Year, System.DateTime.Now.Month));
Adding time to the current date and time
In other programming languages when you had to add a few hours, or even a few seconds to a date/time you had to create an entire algorithm. It wasn’t simple at all. Thanks to .NET you benefit of AddHours, AddMinutes, AddSeconds etc. methods.
Here’s an example where seconds, minutes, days and months are added to the current date.
Console.WriteLine("Current date and time: {0}", System.DateTime.Now);
Console.WriteLine("Added 15 seconds: {0}", System.DateTime.Now.AddSeconds(15));
Console.WriteLine("Added 32 minutes: {0}", System.DateTime.Now.AddMinutes(32));
Console.WriteLine("Added 8 days: {0}", System.DateTime.Now.AddDays(8));
Console.WriteLine("Added 5 months: {0}", System.DateTime.Now.AddMonths(5));
This is how the output looks on my computer:
Current date and time: 9/18/2004 9:29:33 Added 15 seconds: 9/18/2004 9:29:48 Added 32 minutes: 9/18/2004 10:01:33 Added 8 days: 9/26/2004 9:29:33 Added 5 months: 2/18/2005 9:29:33 Press any key to continue |
Look at the result after adding 5 months to the date. Things work properly and we jump in the year 2005, February 18.
Subtracting time from the current date and time
You subtract time by adding negative values. That means you still use the methods AddHours, AddMinutes, AddSeconds etc.
Console.WriteLine("Current date and time: {0}", System.DateTime.Now);
Console.WriteLine("Subtracted 15 seconds: {0}", System.DateTime.Now.AddSeconds(-15));
Console.WriteLine("Subtracted 32 minutes: {0}", System.DateTime.Now.AddMinutes(-32));
Console.WriteLine("Subtracted 8 days: {0}", System.DateTime.Now.AddDays(-8));
Console.WriteLine("Subtracted 5 months: {0}", System.DateTime.Now.AddMonths(-5));
Here’s the result I got on this Sunday morning:
Current date and time: 9/19/2004 10:07:11 Subtracted 15 seconds: 9/19/2004 10:06:56 Subtracted 32 minutes: 9/19/2004 9:35:11 Subtracted 8 days: 9/11/2004 10:07:11 Subtracted 5 months: 4/19/2004 10:07:11 Press any key to continue |