You may first initialize a System.DateTime
which gets the DateTime
from the string you've specified above. Then, you may use System.DateTime.ToUniversalTime()
which converts the value of the current System.DateTime
object to Coordinated Universal Time and store it in the database.
Example
DateTime AEST = DateTime.Parse("1 December 2012, 8:00:00"); //Initialize a new DateTime of name AEST which gets a System.DateTime from 1 December 2012, 8:00:00
DateTime UTC = AEST.ToUniversalTime(); //Initialize a new DateTime of name UTC which converts the value from AEST to Coordinated Universal Time
System.Diagnostics.Debug.WriteLine(UTC.ToString()); //Writes 12/1/2012 6:00:00 AM
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)
where dateTime
is the DateTime
you would like to convert, sourceTimeZone
is the time zone of the DateTime
you would like to convert and destinationTimeZone
is the time zone you would to get from dateTime
Example
string _date = "1 December 2012, 8:00:00"; //Initializes a new string of name _date as "1 December 2012, 8:00:00"
string targetTimeZone = "W. Australia Standard Time"; //Initializes a new string of name targetTimeZone as "W. Australia Standard Time"
DateTime sourceDateTime = DateTime.Parse(_date); //Initializes a new DateTime of name sourceDateTime which gets a valid DateTIme object from 1 December 2012, 8:00:00
DateTime AEST = TimeZoneInfo.ConvertTime(sourceDateTime, TimeZoneInfo.Local, TimeZoneInfo.FindSystemTimeZoneById(targetTimeZone)); //Initializes a new DateTime which converts the time zone from sourceDateTime assuming that sourceDateTime's TimeZone is the local time zone of the machine to an W. Australia Standard Time time zone of name AEST
This will convert 1 December 2012, 8:00:00
assuming that its time zone is the local time zone of the machine (e.g Egypt Standard Time: (GMT+02:00) Cairo
) to W. Australia Standard Time: (GMT+08:00) Perth
which will be 12/1/2012 2:00:00 PM
.
For list of time zones, see Microsoft Time Zone Index Values
Thanks,
I hope you find this helpful :)