DateTime Refresher

How often do you use the DateTime object in .NET?

I’m willing to bet quite often, but here’s the thing. I reckon, like most .NET developers, most of the time you use it to do the following:

var currentDate = DateTime.Now;


var currentDate = DateTime.UtcNow;

What many developers don’t stop to look at, however, is just what the humble DateTime object hides under its hood. Closer to the surface, it has the common things you might expect, such as ‘ToString’ allowing you to do things like this:

var rfc1123string = DateTime.Now.ToString("r");

which will give you a date formatted as follows

"Thu, 19 Mar 2015 16:13:03 GMT"

( Not bad for one format letter 🙂 )

Or, you can tweak the format yourself by using a longer format specifier.

var myFormat = DateTIme.Now.ToString("dddd dd, hh:mm:ss - MMMM yyyy");

That will give you a date the looks like this:

"Thursday 19, 04:20:39 - March 2015"

A quick Google search for “Date Formatting C# Cheat Sheet” will get you a number of handy quick references great to pin on your office wall, a worthwhile investment of a sheet of A4.

There are also a couple of quick to use handy extensions:

// 16:31
var shortTime = DateTime.Now.ToShortTimeString();
// 19/03/2015
var shortDate = DateTime.Now.ToShortDateString();
// 16:32:50
var longTime = DateTime.Now.ToLongTimeString();
// 19 March 2015
var longDate = DateTime.Now.ToLongDateString();

The quick extensions are affected by your local settings. The comments in the preceding code are for my territory which is “en-gb”, but the output for other territories should be similar.

If pretty string printing were the only thing DateTime could do, however, we’d not have very much use for it. Fortunately for us, it’s also very good at performing arithmetic and, when coupled with the “TimeSpan” object, can be used to make calculations very easy indeed.

Let’s say, for example, we wanted to know what the day would be exactly 1 year from today, we could easily find this out by using the following:

var dateInOneYearsTime = DateTime.Now + new
   TimeSpan(365, 0, 0, 0);
var theDayInOneYearsTime = dateInOneYearsTime.ToString("dddd");
Console.WriteLine("Today is : {0}", DateTime.Now.ToString("r"));
Console.WriteLine("The day 1 year from today will be : {0}",

We know that there are 365 days in a year, and the constructor for a time span takes the form (Days, Hours, Minutes, Seconds), allowing us to easily add 365 days onto our current time. We then use ToString or the properties of the result to get the properties we want to access.

TimeSpan can be used to perform all sorts of time period arithmetic, which then can be easily applied to a DateTime object. It comes in quite handy for conversions, too; for example, we easily can find out how many seconds there are in 15 minutes by using the following code:

var totalMinutes = new TimeSpan(0, 0, 15, 0);
var totalSeconds = totalMinutes.TotalSeconds;
Console.WriteLine("Total number of seconds in 15 minutes
   is {0}", totalSeconds);

Which, as you can see, is 900.

We easily can do the same to get the total number of days, hours, milliseconds, and minutes.

You don’t have to use a TimeSpan, however; you can perform math operations directly on DateTime objects. If we wanted to know how many minutes were between two time periods, we can find this out as follows:

// 10th January 2015 at 10am
var firstDate = new DateTime(2015, 1, 10, 10, 0, 0);
// 5th March 2015 at 4:30pm
var secondDate = new DateTime(2015, 3, 5, 16, 30, 00);
var totalMinutes = (secondDate - firstDate).TotalMinutes;
Console.WriteLine("The total number of minutes between {0}
   and {1} is {2}", firstDate.ToString("r"),
   secondDate.ToString("r"), totalMinutes);

Which, as you can see, is 78150.

You also can do most of this arithmetic just using the various “Add” methods directly on your single object.

If you were building a web page and that web page was required to allow the user to pick from a list of valid date formats supported by their system, you’d need a way to interrogate the system to decide which formats it can support. This is easily done by using the following:

var formats = DateTime.Now.GetDateTimeFormats();

Which, on my system, gives me a string array containing 89 different variations.

If you need to make decisions based on daylight savings time, you can do so by using the following:


The property ‘Kind’ on the date time object will return an enumerated value that tells you if the time value is ‘Local’ or ‘Universal’, thus allowing you to make decisions on needing to convert to/from your local time zone.

The DateTime object pack a lot of power under the surface, and makes it very easy to do almost anything you might want involving date and time manipulation, so next time you only use it to get the current time, spare a thought for what other tasks you might be able to put it to.

Got a .NET question you want answered, or want to know if there’s a .NET way of doing a task you’ve been assigned? Leave a comment below, or come and find me in the “Lidnug” users group on Linked-In and ask; there’s a good chance it’ll end up as a post in this column.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read