C#中的时区转换 [英] Time zone conversion in C#
问题描述
我有一个日期格式,类似于:
I have a date format, something similar to:
星期一,2009年8月11日13:15:10 GMT
Mon, 11 Aug 2009 13:15:10 GMT
如何将其转换为EST格式?
How do I convert this to EST format?
推荐答案
这个或者类似的东西应该是诀窍:
This, or similar should do the trick:
var dateString = "Tue, 11 Aug 2009 13:15:10 GMT";
var date = Convert.ToDateTime(dateString);
var result = TimeZoneInfo.ConvertTime(date, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
实际上是不正确的,所以我已经把它改成了2009年8月11日星期二,所以代码将会运行,因为 Convert.ToDateTime
抛出异常如果一天不符合日期。
It's worth mentioning, that your originally specified Mon, 11 Aug 2009, is actually incorrect, hence I've changed it to Tue, 11 Aug 2009 so the code will run, as the Convert.ToDateTime
throws an exception if the day does not match the date.
我也假设你的意思是东部标准时间
,这是与东部时间美国和加拿大),但您可以通过运行以下代码获取可用时区的完整列表:
I've also assumed that you mean Eastern Standard Time
, which is the id associated with "Eastern Time (US & Canada)", but you can get a full list of the time zones available by running the following code:
foreach(TimeZoneInfo info in TimeZoneInfo.GetSystemTimeZones())
{
Console.WriteLine("Id: {0}", info.Id);
Console.WriteLine(" DisplayName: {0}", info.DisplayName);
}
这篇关于C#中的时区转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!