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:
Now hit F5 and you should see the current date and time print out like this:
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:
This will simply get the current date with no time. For us, this prints out the following:
This is the short date, but we can get the date in a number of ways. Try these two examples as well:
This will get the date, but this time it will print it out as 26 July 2012. You can also do it this way:
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.
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:
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”):
This will get 4 for us.
This time we will get the month. Copy the following:
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:
This will get the month in string.
Now we will get the year, and is a straightforward like above:
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:
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.
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.
This will get the hour and minutes but no second.
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:
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:
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|
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.