从的TimeZoneInfo时区偏移分钟 [英] TimeZoneInfo from timezone minutes offset

查看:666
本文介绍了从的TimeZoneInfo时区偏移分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是JavaScript的我已经过去了,到控制器,用户的客户端的日期和时间是从UTC用<$ C $方法使用getTimezoneOffset 偏移分钟数C>日期对象。现在,我已经在服务器端这些信息,我想从中创建的TimeZoneInfo。这怎么可能?如果这是不可能的,那么我怎么才能将UTC在服务器端日期到使用分钟内客户端的时区偏移?


解决方案

  

我想从它创建的TimeZoneInfo。这怎么可能?


这是不可能的。 A 时区偏移量的是不一样的事情作为一个的时区的。请阅读时区标签维基,特别是标题为时区!=偏移。


  

...然后我如何转换UTC在服务器端日期到使用分钟内客户端的时区偏移?


创建一个的DateTimeOffset ,假以时日再presents那一刻。例如:

  //从数据库。请确保您指定的UTC的那种。
UTC日期时间=新日期时间(2013,1,1,0,0,0,DateTimeKind.Utc);//从JavaScript的
INT offsetMinutes = 420;//不要忘了这里反转迹象
时间跨度偏移量= TimeSpan.FromMinutes(-offsetMinutes);//最后的结果
DTO的DateTimeOffset =新的DateTimeOffset(UTC).ToOffset(偏移);

此外,还要确保你明白,你在JavaScript中的客户端检索到的偏移不一定正确的偏移量应用到数据库的日期。当你得到的偏移量,它必须在特定时刻。由于许多时区偏移更改为夏令时,你的无法的假设您目前拥有的偏移量是适合于您的数据库中的任何特定值。因此,在上述code做了你问什么,它可能仍然不是一般的好主意。

From JavaScript I have passed, to the controller, the number of minutes that the user's client date time is offset from UTC using the method getTimezoneOffset on the Date object. Now that I have this information on the server side I'd like to create a TimeZoneInfo from it. How is this possible? If this is not possible then how can I convert UTC dates on the server side into the client's timezone using the minutes offset?

解决方案

I'd like to create a TimeZoneInfo from it. How is this possible?

It's not possible. A time zone offset is not the same thing as a time zone. Please read the timezone tag wiki, especially the section titled "Time Zone != Offset".

... then how can I convert UTC dates on the server side into the client's timezone using the minutes offset?

Create a DateTimeOffset that represents that moment in time. For example:

// From your database.  Make sure you specify the UTC kind.
DateTime utc = new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc);

// From JavaScript
int offsetMinutes = 420;

// Don't forget to invert the sign here
TimeSpan offset = TimeSpan.FromMinutes(-offsetMinutes);

// The final result
DateTimeOffset dto = new DateTimeOffset(utc).ToOffset(offset);

Also, make sure you understand that the offset you retrieved from the client in JavaScript is not necessarily the correct offset to apply to your database date. When you get the offset, it has to be for a particular moment in time. Since many time zones change offsets for daylight saving time, you cannot assume that the offset you currently have is appropriate for any particular value in your database. Therefore, while the above code does what you asked, it is probably still not a good idea in general.

这篇关于从的TimeZoneInfo时区偏移分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆