Say I have the string "1 December 2012, 8:00:00"
I know that it is AEST time (regardless where the server is), with whatever daylight saving state at that time (1st Dec), but I want to store it in the database as UTC.
How do I convert it to UTC, treating the string as AEST regardless where the server is?
You may first initialize a
System.DateTimewhich gets theDateTimefrom the string you’ve specified above. Then, you may useSystem.DateTime.ToUniversalTime()which converts the value of the currentSystem.DateTimeobject to Coordinated Universal Time and store it in the database.Example
Notice: If you are willing to convert a time from one time zone to another, you may use
TimeZoneInfo.ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo, destinationTimeZone)wheredateTimeis theDateTimeyou would like to convert,sourceTimeZoneis the time zone of theDateTimeyou would like to convert anddestinationTimeZoneis the time zone you would to get fromdateTimeExample
This will convert
1 December 2012, 8:00:00assuming that its time zone is the local time zone of the machine (e.gEgypt Standard Time: (GMT+02:00) Cairo) toW. Australia Standard Time: (GMT+08:00) Perthwhich will be12/1/2012 2:00:00 PM.For list of time zones, see Microsoft Time Zone Index Values
Thanks,
I hope you find this helpful 🙂