说我有字符串"1 December 2012, 8:00:00"
我知道它是AEST时间(无论服务器在哪里),当时的任何夏令时状态(12月1日),但我想将它作为UTC存储在数据库中。
如何将其转换为UTC,将字符串视为AEST,无论服务器在哪里?
答案 0 :(得分:3)
假设您可以使用DateTime.TryParse
解析日期并将其转换为DateTime
结构,则可以调用ToUniversalTime
转换为UTC。
例如:
DateTime dt;
DateTime utcDate;
if (DateTime.TryParse(dateTimeString, out dt))
{
utcDate = dt.ToUniversalTime();
// store utcDate in database
}
else
{
// error, unable to parse the date
}
当然假设您的本地时间是AEST,或者您正在解析的日期/时间字符串具有时区说明符。
如果您的当地时间不是其他且没有时区说明符,那么您需要解析日期,添加或减去时区的偏移量以匹配AEST,然后拨打ToUniversalTime
。
答案 1 :(得分:2)
您可以先初始化一个System.DateTime
,它从您上面指定的字符串中获取DateTime
。然后,您可以使用System.DateTime.ToUniversalTime()
将当前System.DateTime
对象的值转换为协调世界时,并将其存储在数据库中。
示例强>
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
注意:如果您愿意将时间从一个时区转换为另一个时区,则可以TimeZoneInfo.ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo, destinationTimeZone)
dateTime
使用DateTime
sourceTimeZone
要转换,DateTime
是您要转换的destinationTimeZone
的时区,dateTime
是您从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
获得的时区
示例强>
1 December 2012, 8:00:00
这将转换Egypt Standard Time: (GMT+02:00) Cairo
,假设其时区是计算机的本地时区(例如W. Australia Standard Time: (GMT+08:00) Perth
)到12/1/2012 2:00:00 PM
,这将是{{1}}。
有关时区列表,请参阅Microsoft Time Zone Index Values
谢谢, 我希望你觉得这很有帮助:)
答案 2 :(得分:1)
感谢Picrofo EGY,esr和White Dragon,我创建了这个解决它的功能
public static DateTimeOffset CreateDateWithTimezone(string dateStr, TimeZoneInfo tzi)
{
DateTimeOffset dtoTzi = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, tzi);
DateTimeOffset dto;
if (!DateTimeOffset.TryParse(dateStr + " " + dtoTzi.ToString("zzz"), out dto))
throw new Exception("Failed to parse date: " + dateStr + " " + dtoTzi.ToString("zzz"));
if (tzi.SupportsDaylightSavingTime)
{
TimeSpan offset = tzi.GetUtcOffset(dto);
string offsetStr = (offset.TotalHours < 0 ? "" : "+") + offset.Hours.ToString("00") + ":" + offset.Minutes.ToString("00");
if (!DateTimeOffset.TryParse(dateStr + " " + offsetStr, out dto))
throw new Exception("Failed to parse date: " + dateStr + " " + dtoTzi.ToString("zzz"));
}
return dto;
}
用法:
string dateStr = "1 December 2012, 08:00:00";
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
DateTimeOffset dtoUtc = CreateDateWithTimezone(dateStr, tzi).ToUniversalTime();
Response.Write("dtoUtc is " + dtoUtc);
它会打印出来
dtoUtc是30/11/2012 9:00:00 PM +00:00
完美!