Is there any function for UTC to convert to specific time and time in UTC

I want to convert for example a specific date 12-11-2008 11: 33: 04.510 to UTC datetime. Can anyone help me how to do this. I want to do it in C # encoding.

0


source to share


3 answers


Just use DateTime.ToUniversalTime , assuming it's in your computer's local time zone at the moment.



+8


source


DateTime does not respect the time zone. It can be thought of as local time or UTC time, and as Jon Skeet said DateTime.ToUniversalTime can convert between the two.



.NET3.5 also has a TimeZoneInfo class that lets you convert DateTimes between arbitrary time zones, but for your needs the former is probably good enough. There is also a DateTimeOffset class that works exactly like DateTime, except that it also preserves the offset from UTC, making it a little more reliable if you have to handle multiple time zones.

+2


source


If you want DateTime to be identified as UTC, you can also assign DateTimeKind to it,

DateTime saveNow = DateTime.Now; DateTime myDt; myDt = DateTime.SpecifyKind (saveNow, DateTimeKind.Utc);

Or, if you know it locally: the string formattedDate = "12-11-2008 11: 33: 04.510"; DateTime localDt = DateTime.Parse (formattedDate, null, DateTimeStyles.AssumeLocal);

Or, if you know UTC: string formattedDate = "12-11-2008 11: 33: 04.510"; DateTime localDt = DateTime.Parse (formattedDate, null, DateTimeStyles.AssumeUniversal);

0


source







All Articles