开发者

DateTime from user conversion?

开发者 https://www.devze.com 2023-02-05 15:16 出处:网络
I\'m getting a time from a use as a string. This time is assumed to be in Eastern time. I need store this in the database as UTC time. How do I do t开发者_运维知识库his?

I'm getting a time from a use as a string. This time is assumed to be in Eastern time. I need store this in the database as UTC time. How do I do t开发者_运维知识库his?

DateTime.SpecifyKind doesn't accept Eastern. In another thread, I read something about using DateTimeOffset


From MSDN:

DateTime easternTime = new DateTime(2007, 01, 02, 12, 16, 00);
string easternZoneId = "Eastern Standard Time";
try
{
   TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById(easternZoneId);
   Console.WriteLine("The date and time are {0} UTC.", 
                     TimeZoneInfo.ConvertTimeToUtc(easternTime, easternZone));
}
catch (TimeZoneNotFoundException)
{
   Console.WriteLine("Unable to find the {0} zone in the registry.", 
                     easternZoneId);
}                           
catch (InvalidTimeZoneException)
{
   Console.WriteLine("Registry data on the {0} zone has been corrupted.", 
                     easternZoneId);
}


http://msdn.microsoft.com/en-us/library/bb397769.aspx

DateTime dateNow = DateTime.Now;
Console.WriteLine("The date and time are {0} UTC.", 
                   TimeZoneInfo.ConvertTimeToUtc(dateNow));

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号