datetime - C# How to convert UTC date time to Mexico date time -
see code used convert mexico date , time utc date , time.
string strdatetime = "25/01/2017 07:31:00 am"; datetime localdatetime = datetime.parse(strdatetime); datetime univdatetime = localdatetime.touniversaltime();
touniversaltime
return utc 25-01-2017 02:01:00
when again try convert same utc date , time utc 25-01-2017 02:01:00
mexico local time got 24-01-2017 06:01:00
so see 07:31:00 becomes 06:01:00
not right. tell me missing in code getting wrong local time when convert utc mexico time using timezone info.
see code converting utc mexico local time using timezone info.
string strdatetime = "25-01-2017 02:01:00"; datetime utcdatetime = datetime.parse(strdatetime); string nztimezonekey = "pacific standard time (mexico)"; timezoneinfo nztimezone = timezoneinfo.findsystemtimezonebyid(nztimezonekey); datetime nzdatetime = timezoneinfo.converttimefromutc(utcdatetime, nztimezone);
you current time zone (utc+05:30)
different time zone converting (utc-8:00)
. difference. there 13 hours , 30 minutes difference original time zone targeted one. 5:30 - (-8) = 13:30
.
subtract 13 hours , 30 minutes original date, , 18:01:00
, in 12-hour format 6pm
on previous day.
edit:
instead of hard-coding mexico time zone, need have method can determine user's time zone no matter coming from. best done using javascript outlined in this answer.
Comments
Post a Comment