简体   繁体   English

从JavaScript中的.NET DateTime JSON格式字符串获取正确的日期值

[英]Get correct date value from .NET DateTime JSON format string in Javascript

I have a javascript application which receives data from ASP.NET WebService in JSON format. 我有一个JavaScript应用程序,该应用程序以JSON格式从ASP.NET WebService接收数据。 My application has a lot of manipulations with dates which it also receives from WebService. 我的应用程序有很多带有日期的操作,它也从WebService那里接收。

WebService stores all dates in EST timezone and sends them in such format: WebService将所有日期存储在EST时区中,并以以下格式发送:

{"Date":"\/Date(1319205600000+0300)\/"} 

to my javascript application. 到我的javascript应用程序。

On the client side I should display all dates also in EST timezone irrespectively of browser's timezone. 在客户端,无论浏览器所在的时区如何,我都应在EST时区中显示所有日期。 So if I receive from the server the representation of: 因此,如果我从服务器收到以下表示:

10/21/2011 10:00 2011年10月21日10:00

I should display exactly the same time to the user. 我应该向用户显示完全相同的时间。

So to convert dates I do something like this: 因此,要转换日期,我会执行以下操作:

function convert_date(millisec) {
    var date = new Date(millisec),
        local_offset = date.getTimezoneOffset(),
        server_offset = -5 * 60, //EST offset
        diff = (local_offset + server_offset) * 60000;

    return new Date(millisec + diff);
}

But the point is that server_offset not always should be -5. 但是关键是server_offset并不总是应该为-5。 It can be -4 depending on DST value. 根据DST值,它可以为-4。 I've tried to do server_offset = (date.isDST() ? -4 : -5) * 60 instead but I haven't found any solution for capturing isDST() which works fine for all the local client timezones. 我试图做server_offset = (date.isDST() ? -4 : -5) * 60但是我没有找到任何捕获isDST()解决方案,该解决方案对于所有本地客户端时区都适用。 Most of them work fine for that local browser's timezone which has the same value of DST as EST timezone but would fail in the case of GMT+5:30 timezone for example. 它们中的大多数都适用于本地浏览器所在的时区,该时区的DST与EST时区的值相同,但是在GMT + 5:30时区可能会失败。

So are there any way to determine whether DST would be applied for some specific date in EST timezone from javascript? 那么,有什么方法可以确定是否从javascript的EST时区中的某个特定日期开始应用DST?

Or maybe I've missed something? 也许我错过了什么?

If you can modify the web service, I would have it also return a flag indicating whether or not it was daylight saving time in the server's timezone (EST). 如果您可以修改Web服务,那么我还将让它返回一个标志,指示该时间是否为服务器时区(EST)中的夏令时。

Assuming you can't do this, you can determine whether it is daylight saving time according to this information for DST in the United States : 假设您无法执行此操作,则可以根据以下DST在美国的信息确定是否为夏令时:

DST starts on the second Sunday of March and it ends on the first Sunday of November. DST从3月的第二个星期日开始,到11月的第一个星期日结束。

The caveat being that this could change in the future (I wasn't even aware it had changed in 2007). 需要注意的是,这种情况将来可能会改变(我什至不知道它在2007年有所改变)。

I am not really sure how exactly you are getting the ticks value you pass to the function, which I think you have something weird going on there. 我不确定您传递给该函数的ticks值的准确程度如何,我认为您在此发生了一些奇怪的事情。 But if the question is how to write the isDST() function, then that should be pretty simple. 但是,如果问题是如何编写isDST()函数,那么这应该很简单。

Date.prototype.isDST = function()
{
    var dt = new Date(this.getYear(), 1, 1);
    return dt.getTimezoneOffset() > this.getTimezoneOffset();
}

edit: This isn't polished/tested for negative offsets. 编辑:这不是抛光/负偏移测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM