用户转换的日期时间?

时间:2011-01-20 20:42:34

标签: c# .net datetime

我从使用字符串中获得了一些时间。假设这个时间是在东部时间。我需要将其作为UTC时间存储在数据库中。我该怎么做?

DateTime.SpecifyKind不接受东方。在另一个主题中,我读了一些关于使用DateTimeOffset

的内容

2 个答案:

答案 0 :(得分:3)

来自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);
}

答案 1 :(得分:-1)

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));