Reputation: 1767
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
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
Reputation: 18815
Not built in, but you can create your own formatter (google IFormatProvider)
Upvotes: 1
Reputation: 2544
No, you'll first have to convert the DateTime to the desired time zone.
Upvotes: 0