2015-10-22 61 views
0

我已经试过寻找答案,尽管我找到的答案非常相似,但我不认为它们正是我正在寻找的东西。请原谅我,如果这已经在别处回答。在javascript中解析UTC ISO日期到本地时间日期/ jquery

我想解析JavaScript中的ISO日期,以便我可以将它与客户机系统日期进行比较,并根据ISO日期是在客户机系统日期之前还是之后显示信息。

这很好,直到我需要支持IE8,现在我卡住了。

我创建了一个函数,因为我有三个不同的日期,我需要这样做。

例如,我的ISO日期是:2015-12-22T11:59 UTC时间。

但是,一旦我的日期被解析,在本地时间的全天时间是11:59,无论我测试哪个时区,它在该时区总是11.59。

我知道我创建的函数目前没有对时区做任何事情,这是我卡住的地方。我不知道要添加什么才能将我的结束日期更改为客户机时区的反映。

任何帮助或建议将不胜感激。 我无法使用像moments.js这样的内容,因为我有上传限制。

虽然Jquery可用。或纯javascript。

<script> 
function setSaleContent() { 


    //creating a new date object that takes the clients current system time. so we can compare it to the dates stored in our array 
    var currentDate = new Date(); 
    console.log(currentDate + " this is the clients date "); 


    //These variables actually come from an external array object, but I'm putting them in here like this for this example. 
    var destinations = { 
     freedate: "2015-12-16T11:59", 
     courierdate: "2015-12-22T11:59", 
     nextdaydate: "2015-12-23T11:59", 
    } 

    //fetch all the ISO dates from the array. 
    var freeDateISO = destinations["freedate"]; 
    var courierDateISO = destinations["courierdate"]; 
    var nextdayDateISO = destinations["nextdaydate"]; 


    //I am creating this reusable function to split up my ISO date for the sake of IE8.. and create it into a date format that can be compared against another date. I know that this isn't doing anything with my timezone and that is where my problem lies. 
    function parseDate(str) { 
     var parts = /^(\d{4}).(\d{2}).(\d{2}).(\d{2}):(\d{2})/.exec(str); 
     if (parts) { 
      return new Date(parts[1], parts[2] - 1, parts[3], parts[4], parts[5]); 
     } 
     return new Date(); 
    } 


    //I would like this date to change to reflect the time zone of the clients system time. 
    //currently returns the date at 11.59 regardless of timezone. 
    //If i was in GMT i would want it to say 11.59 
    //If i was in CT time I would like this to say 05.59 
    //If i was in Perth I would like this to say 19:59 
    var freeDate = parseDate(freeDateISO); 
    console.log(freeDate + " this is the converted date for IE") 





} 


window.onload = setSaleContent; 

+0

不要使用'new Date(...)'(它从本地值创建日期时间),使用'new Date(Date.UTC(...))'作为utc值! – Bergi

回答

0

的简单的解决方案是要追加Z到ISO日期,以表明它是在UTC时间,如2015-12-22T11:59Z

当JavaScript将该日期解析为字符串时,它会自动将UTC日期转换为本地时区。

虽然这很简单,但形式为new Date(str);的解析调用足够简单,但对于使用针对IE8和其他旧浏览器的数字参数的解析调用,它不会很好。

一种解析ISO日期与时区填充工具存在:Javascript JSON Date parse in IE7/IE8 returns NaN
这可以做一些修改后更换您的自定义功能parseDate采取一个输入字符串。

或者,使用新创建的日期中的.getTimezoneOffset()方法来实现您自己的自定义日期操作符来计算当地时区,该日期以分钟为单位给出时区偏移量,但您必须想出一种利用由于JavaScript日期对象的方法有限,因此需要调整时间和分钟等偏移量。

+0

我试图将Z添加到它之前,并且当我正在执行新的Date(str)时,它完美地工作。但是这在IE8中不起作用。 – w3littlebird

+0

我预计这会发生是因为您的自定义解析代码为IE8。就像我在答案中所说的那样,如果你不能使用任何库,你必须用'.getTimezoneOffset()'做些事情并实现你自己的日期操作方法。你可以尝试用'+00:00'而不是'Z'来追加它,看看IE8是否可以将它解析为一个字符串,但我怀疑是否有更好的解决方案。或者,请参阅此IE7/8 polyfill解析ISO日期:http://stackoverflow.com/questions/11020658/javascript-json-date-parse-in-ie7-ie8-returns-nan – Ryan

+0

对不起,试图在iPad上回复并搞砸了我的评论。我试图将Z附加到它之前,当我在做新的Date(str)并且完成我所需要的时候,它完美地工作,但是这在IE8中不起作用。这就是我创建parseDate函数的原因。我知道我的函数没有对日期部分做任何事情来获取时区,我不知道如何写它,或者如果我需要做一些getTimeZoneOffset计算。但无论我需要做什么,我都不知道要写什么才能实现它,而且我已经尝试了几个小时并搜索了几个小时。 – w3littlebird

相关问题