Format Date/Time as UTC with Timezone Offset

Here’s a quick snippet from MSDN that demonstrates how to format a date/time to UTC format with a timezone offset. (ISO 8601 compliant!)

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
DateTimeOffset dateOffset = new DateTimeOffset(date1,
    TimeZoneInfo.Local.GetUtcOffset(date1));
Console.Write(dateOffset.ToString("o"));

This will produce the following output:

2008-04-10T06:30:00.0000000-04:00

Everything you ever wanted to know and more about date and time formatting can be found here.

Advertisements

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s