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.

Advertisement

Author: Adam Prescott

I'm enthusiastic and passionate about creating intuitive, great-looking software. I strive to find the simplest solutions to complex problems, and I embrace agile principles and test-driven development.

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: