Sanjay10
Sanjay10

Reputation: 1767

.NET 2.0 DateTime.ToString() conversion to different time zone

is there a format in ToString() method of DateTime to convert the time Zone to say UTC ?

I know I can programatically first convert the DateTime to UTC and then call ToString, but I have a UI where the user can specify format, can they at the same time convert to UTC ?

Upvotes: 0

Views: 2127

Answers (3)

John
John

Reputation: 30518

The .ToString("u") will format as UTC but not convert. This code below will convert and present the Date and Time in UTC format:

System.TimeZone.CurrentTimeZone.ToUniversalTime(Date.Now).ToString("u")

or

DateTime.Now.ToUniversalTime().ToString("u")

other formats can be found here

Upvotes: 4

Tim Jarvis
Tim Jarvis

Reputation: 18815

Not built in, but you can create your own formatter (google IFormatProvider)

Upvotes: 1

Christopher Garcia
Christopher Garcia

Reputation: 2544

No, you'll first have to convert the DateTime to the desired time zone.

Upvotes: 0

Related Questions