DateTime and TimeSpan

Ignoring the cultrual subtleties of how different people mark their calendars, C# includes a relatively easy way to store and manipulate dates and time, even if their initialization is a bit clunky. The DateTime structure represents a number of attributes that are used to describe a specific date and... time. You can create such a structure with code such as:

DateTime myBirthday = new DateTime("1986", "1", "27", "0", "0", "0");

Where the arguments are (in order from left to right): the year, month, day, hour, minute, and second, of a particular point in time. When converted to a string, the following DateTime would be shown as "1/27/1986 12:00:00 AM", using the US format for dates. Hours utilize "military time", in that 0 is midnight, and 13 is 1 PM. Elsewhere in the world, you would probably see the month and day reversed: 27/1/1986. Which does logically make sense — listing the parameters of time in increasing significance — but sadly looks like nonsense to my American brain. To insist on alternate formats, you can provide arguments to a System.Globalization object, such as:

myBirthday.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR"))

So that it prints out as 27/01/1986 00:00:00. You can also use "F" as an argument to ToString to output the date/time as:

Monday, January 27, 1986 12:00:00 AM

This helps simplify the process of formatting dates to any specific standard.

Now, what's supremely good about the DateTime is how you can use arithmetic on them. You can add or subtract time by providing positive or negative values to a series of Add methods they have. But you can also determine the time that spans between two DateTime structures by simply subtracting one from another! So for example:

TimeSpan yearsOld = DateTime.Now - myBirthday;
Console.WriteLine("I am currently " + (int)(yearsOld.TotalDays / 365.25) + " years old!");

would print out that I am 33 years old — at least as of when I wrote these notes, on January 16th, 2020.