.NET 到 JavaScript 的纪元时间(休息一小时?)

发布于 2024-12-08 08:53:19 字数 748 浏览 0 评论 0原文

在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭水袖舞倾城 2024-12-15 08:53:19

在我看来,unix 纪元是 1970 年 1 月 1 日,UTC。

鉴于此,您创建日期然后转换为当地时间有点倒退。您需要做的是将变量时间值转换为 UTC。

<Extension()>
Public Function ToEpoch(value As Date) As Double
    Dim span As TimeSpan = (value.ToUniversalTime - 
                                New System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))
    Return span.TotalMilliseconds
End Function

您可能认为这两个转换是等效的,但它们可能不是,如中所述
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.

<Extension()>
Public Function ToEpoch(value As Date) As Double
    Dim span As TimeSpan = (value.ToUniversalTime - 
                                New System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))
    Return span.TotalMilliseconds
End Function

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 .

蓝天 2024-12-15 08:53:19

我怀疑这两个日期具有不同的夏令时值。查看以下对 IsDaylightSavingTime() 的调用是否返回相同的值:

Dim dt As Date = new Date(2011, 9, 14)
Dim epoch As Date = new Date(1970, 1, 1)

dt.IsDaylightSavingTime()
epoch.IsDaylightSavingTime()

I suspect the two dates have different daylight savings values. See if the following calls to IsDaylightSavingTime() return the same values:

Dim dt As Date = new Date(2011, 9, 14)
Dim epoch As Date = new Date(1970, 1, 1)

dt.IsDaylightSavingTime()
epoch.IsDaylightSavingTime()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文