.NET转换日期时间为UTC给出的时区 [英] .NET converting datetime to UTC given the timezone
问题描述
使用C#,我需要进入的日期时间值转换为UTC。我知道有功能在.NET中的这些转换,但我必须确定时区的标准时区列表
Using C#, I need to convert incoming datetime values into UTC. I know there is functionality in .NET for these conversions but all I have to identify the timezone is the standard timezone list
http://www.timegenie.com/timezones
什么是做到这一点的。NET的最好方法是什么?我需要创建一个映射表的时区转换为标识的TimeZoneInfo.GetSystemTimeZones()(例如,太平洋标准时间(墨西哥)) 这样我就可以使用TimeZoneInfo.FindSystemTimeZoneById()?
What's the best way to do this in .NET? Will I need to create a mapping table to convert the timezones to the IDs in TimeZoneInfo.GetSystemTimeZones() (e.g. "Pacific Standard Time (Mexico)") so that I can then use TimeZoneInfo.FindSystemTimeZoneById()?
感谢您的帮助
推荐答案
我以前用映射表存放时区ID在数据库中做到了。即包含的成绩表 TimeZone.GetSystemTimeZones()
I've done it before by storing the timezone id in the database using a mapping table. i.e. A table containing the results of TimeZone.GetSystemTimeZones()
您实际上并不需要使用 TimeZoneInfo.FindSystemTimeZoneById()
虽然:你可以使用的TimeZoneInfo的重载之一的转换。 ConvertTimeBySystemTimeZoneId()
。这种方法有一些重载内搭DateTime值,以及一些需要的DateTimeOffset值(这是preferable,因为他们指定的时间一个明确的点)。
You don't actually need to use TimeZoneInfo.FindSystemTimeZoneById()
though: you can do the conversion using one of the overloads of TimeZoneInfo.ConvertTimeBySystemTimeZoneId()
. This method has some overloads which take DateTime values, and some that take DateTimeOffset values (which are preferable as they specify an unambiguous point in time).
例如。
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "New Zealand Standard Time", "UTC")
使用该系统时区ID,而不是真正的好处偏移存储在数据库中是夏令时将自动为您处理 TimeZoneInfo.ConvertTimeBySystemTimeZoneId
。
这篇关于.NET转换日期时间为UTC给出的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!