Tuesday, June 10, 2008

XML Schema dateTime Primer for C# Developers

The XML Schema dateTime primitive data type represents the current date and time (June 10th, 2008 at 9:30am Eastern Daylight Savings at the time of this writing) in the following format:

2008-06-10T13:30:00Z

The above format represents the UTC date/time.  UTC, or coordinated universal time, is just Greenwich Mean Time (or Zulu time, hence the "Z").  UTC is the SI name for this zero time offset.  To get the UTC date/time for your local time, simply add your time zone offset.  In Indiana, where I'm from, our GMT offset is currently -04:00 (colloquially, "four hours behind").

Here's a C# one-liner to format the current time in the XML Schema dateTime format.

string xsDateTime = System.DateTime.Now.ToUniversalTime().ToString("o");