.NET 到 JavaScript 的纪元时间(休息一小时?)
在 .NET 中使用以下代码
Input: "2011-09-14 00:00:00.0000000" (From an SQL datebase loaded into a Date datetype becoming #9/14/2011#)
<Extension()>
Public Function ToEpoch(value As Date) As Double
Dim span As TimeSpan = (value - New Date(1970, 1, 1, 0, 0, 0, 0).ToLocalTime)
Return span.TotalMilliseconds
End Function
在 JavaScript 中使用此代码
var StartDate = new Date(<%= StartDate() %>);
产生此输出
var StartDate = new Date(1315922400000);
看来,仅对于此特定输入,StartDate(在 javascript 端)正好相差一小时。
导致 JavaScript 日期时间为: Tue Sep 13 23:00:00 UTC+1000 2011
如果我输入像 Date.Now
这样的值,它似乎可以正常运行。
我想我错过了一些基本的东西?
Using the following code in .NET
Input: "2011-09-14 00:00:00.0000000" (From an SQL datebase loaded into a Date datetype becoming #9/14/2011#)
<Extension()>
Public Function ToEpoch(value As Date) As Double
Dim span As TimeSpan = (value - New Date(1970, 1, 1, 0, 0, 0, 0).ToLocalTime)
Return span.TotalMilliseconds
End Function
And this in JavaScript
var StartDate = new Date(<%= StartDate() %>);
Resulting in this output
var StartDate = new Date(1315922400000);
It appears that only for this specific input the StartDate (on the javascript side) is exactly one hour off.
Resulting in the JavaScript datetime of: Tue Sep 13 23:00:00 UTC+1000 2011
If I input a value like Date.Now
it appears to function correctly.
I assume I'm missing something fundamental?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,unix 纪元是 1970 年 1 月 1 日,UTC。
鉴于此,您创建日期然后转换为当地时间有点倒退。您需要做的是将变量时间值转换为 UTC。
您可能认为这两个转换是等效的,但它们可能不是,如中所述
http://blogs.msdn.com/b/oldnewthing /archive/2003/10/24/55413.aspx 。
Seems to me that unix epoch is Jan 1, 1970, UTC.
In light of that, your creation of the Date and then conversion to local time is somewhat backwards. What you need to do is convert the variable time value to UTC.
You may think the two conversions are equivalent, but they may not be, as explained in
http://blogs.msdn.com/b/oldnewthing/archive/2003/10/24/55413.aspx .
我怀疑这两个日期具有不同的夏令时值。查看以下对
IsDaylightSavingTime()
的调用是否返回相同的值:I suspect the two dates have different daylight savings values. See if the following calls to
IsDaylightSavingTime()
return the same values: