C# Dates And Times

Dates and times are another data type in C#. Occasionally you need to print out the date in your application or web application, for example at the bottom here you can see the last edit date and time. There are many ways you can manipulate dates and times in C#, here we will address the most common ways. Create a new C# console application and name it Dates And Times. Then copy the following code:

Console.WriteLine(DateTime.Now);
Console.ReadLine();

Now hit F5 and you should see the current date and time print out like this:

26/07/2012 18:07:49

This prints out the date and time according to your PC. As we are using the .NetFramework, the format the date and time will print out in will depend on your PC settings.

Getting the date

This time we will get the date and format it in several ways. Copy the following code:

Console.WriteLine(DateTime.Now.ToString("d"));

This will simply get the current date with no time. For us, this prints out the following:

26/07/2012

This is the short date, but we can get the date in a number of ways. Try these two examples as well:

Console.WriteLine(DateTime.Now.ToLongDateString());

This will get the date, but this time it will print it out as 26 July 2012. You can also do it this way:

Console.WriteLine(DateTime.Now.ToShortDateString());

This gets the date in the format above (26/07/2012).

Extract the date

This time we will get different sections of the date, such as the day, month and year.

Day

Console.WriteLine(DateTime.Now.DayOfWeek);

This gets the day of the week,however you can make it shorter. Sometimes days are just printed with their first three ( 3 ) characters, such as Tu. To do this we use the ToString method and then format it so it should look like this:

Console.WriteLine(DateTime.Now.ToString("ddd"));

We don’t use the DayOfWeek property since we format the DateTime.Now. Additionally, you can get the integer value of the day. Thursday is the 4th day of the week , so to do this we write DayOfWeek.ToString(“d”):

Console.WriteLine(DateTime.Now.DayOfWeek.ToString("d"));

This will get 4 for us.

Month

This time we will get the month. Copy the following:

Console.WriteLine(DateTime.Now.Month);

Now this code will actually print out 7 (July is the 7th month in the year). However if you wanted the word ‘July’ then you need to format it like this:

Console.WriteLine(DateTime.Now.ToString("MMMM"));

This will get the month in string.

Year

Now we will get the year, and is a straightforward like above:

Console.WriteLine(DateTime.Now.Year);

This will simply print out the year.

Add (Days, Months, Years)

In addition, we can also add days, months and years to the current date. The code is as follows:

Console.WriteLine(DateTime.Now.AddDays(5).ToLongDateString());

This will add 5 days to the current day. At the end we need to put whether we want a long or short date, because if we don’t clarify, the date and time will be printed out.

Format the date

The date is printed out with the backslash, however you can change that by specifying a certain format. Copy the following:

Console.WriteLine(DateTime.Now.ToString("dd MMMM yyyy"));

This will get the date in the format 26 July 2012 with no backslash.

Getting the time

Next we will simply get the time on its own, and there are two ways to do so. The first method is LongTime and the second is ShortTime.

Console.WriteLine(DateTime.Now.ToLongTimeString());

This will simply get the time, and it will be displayed as HH:MM:SS (hour, minutes, seconds). Next we will get the short hand version, which is simply the hour and minute.

Console.WriteLine(DateTime.Now.ToShortTimeString());

This will get the hour and minutes but no second.

Extract time

Similar to the date we can also get the hour, minutes, and seconds on their own. This is a more simple method and does not require formatting because the time is a integer value. Here are the codes which will get the hour, minutes and seconds on their own:

Console.WriteLine(DateTime.Now.Hour);
Console.WriteLine(DateTime.Now.Minute);
Console.WriteLine(DateTime.Now.Second);

The first code gets the hour, the second gets the minutes and the last one gets the seconds. Additionally you can add hours or seconds on to the current time; see the code below:

Console.WriteLine(DateTime.Now.AddHours(10).ToLongTimeString());

This will add 10 hours to the current time. We must specify which date format we want, long or short. If we don’t clarify it will also print out the date. You can also add seconds and minutes; to do so simply replace AddHours with AddSeconds or AddMinutes.

Formatting the time

You can format the time so it displays as 12 hour instead of 24 hour like this:

Console.WriteLine(DateTime.Now.ToString("hh:mm:ss tt"));

This will let the time display as 12 hour, such as 6:24:32 pm.

Method / Property Name Description
ToLongDateString Prints out the day in the following format: 17 July 2012
ToShortDateString Prints out the date in short format: 17/7/2012
ToLongTimeString Gets the current time in the format: HH:MM:SS (hour, minutes, seconds)
ToShortTimeString Gets the current time in the format: HH:MM (hour and minutes)
Now Gets the current date and time
Day Gets the current day as an integer (E.g. today is 27 July so it will get 27)
Year Gets the year
Month Gets the month as an integer
DayofWeek Gets the day
Hour Gets the current hour
Minute Gets the current minute
AddHours Adds hours to the current time
AddDays Adds days to the current day
AddMonth Adds months to the current month
AddYear Adds year to the current year

Summary

Dates and times are easy to work with in C#, and there are many ways to format the date and time. This tutorial provided you with a starting point. Remember that DateTime is also a data type.