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



Question:

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 ?


Solution:1

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


Solution:2

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


Solution:3

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »