TimeZoneInfo.ConvertTimeFromUtc c# [英] TimeZoneInfo.ConvertTimeFromUtc c#
问题描述
var Result1 = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.Local)
var Result2 = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id));
这两者都将Utc时间转换为本地时间,这两种方法在结果或性能上有什么区别吗?
Both this convert Utc time to local time, is there any difference between this 2 different ways in result or performance?
哪个好?
why Microsoft not using TimeZoneInfo.Local directly(as for Result1) in sample here
推荐答案
唯一的区别是,第二种方法是绕行,方法是获取一个 TimeZoneInfo
对象,获取该对象的ID,然后查找再次使用ID来访问同一对象.
The only difference is that the second takes a detour by getting a TimeZoneInfo
object, getting the id for it, and looking up the same object again using the id.
示例中的代码使用 FindSystemTimeZoneById
方法的原因是,它正在获取已知ID的 TimeZoneInfo
对象,而不是从<它已经具有的code> TimeZoneInfo 对象.
The reason that the code in the example is using the FindSystemTimeZoneById
method is that it's getting the TimeZoneInfo
object for a known id, it's not getting that id from a TimeZoneInfo
object that it already has.
这篇关于TimeZoneInfo.ConvertTimeFromUtc c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!