知识大全 使用.NET Framework中新的日期时间类型[7]

Posted 时区

篇首语:实践是知识的母亲,知识是生活的明灯。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 使用.NET Framework中新的日期时间类型[7]相关的知识,希望对你有一定的参考价值。

使用.NET Framework中新的日期时间类型[7]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  时区支持

  在 NET Framework 之前 我们只能使用TimeZone来表示一个时区 但是Timezone功能很有限 它只能识别本地时区 可以在UTC和本地时间之间转换时间 而TimeZoneInfo 对TimeZone进行了很大的增强 它可以表示世界上的任意时区 看下面一段代码

static void Main(string[] args)  TimeZone timeZoneA = TimeZone CurrentTimeZone;  Console WriteLine(timeZoneA StandardName);  TimeZoneInfo timeZoneB = TimeZoneInfo Local;  Console WriteLine(timeZoneB StandardName);  TimeZoneInfo timeZoneC = TimeZoneInfo Utc;  Console WriteLine(timeZoneC StandardName);

  

  输出结果如下图所示

  TimeZone提供的属性和方法非常有限 TimeZoneInfo在这方面就显的非常丰富 我们可以使用TimeZoneInfo在两个不同的时区之间转换时间 如下面的代码

static void Main(string[] args)  DateTimeOffset chinaDate = DateTimeOffset Now;  DateTimeOffset easternDate = TimeZoneInfo ConvertTime(    chinaDate     TimeZoneInfo FindSystemTimeZoneById( Eastern Standard Time ));  Console WriteLine( Now: chinaDate);  Console WriteLine( Now in Eastern: easternDate);

  输出结果如下图所示

  这里使用FindSystemTimeZoneById方法来根据ID来获取时区 在推出TimeZoneInfo之后 在以后的开发中完全可以放弃TimeZone类了 TimeZoneInfo已经完全包含了它

  总结

  本文介绍了 NET Framework中对于日期时间类型的支持 希望对大家有所帮助

cha138/Article/program/net/201311/14871

相关参考