简体   繁体   English

JavaScript跨浏览器日期比较?

[英]JavaScript cross-browser date comparison?

I'm trying to keep in local storage with jQuery plugin jStorage some chunk of HTML and also date and time when that chunk is inserted in local storage, so based on time comparation, if 5 minutes passed, this will be updated in local storage. 我正在尝试使用jQuery插件jStorage保留一些本地HTML以及该块插入本地存储中的日期和时间,因此基于时间比较,如果经过5分钟,它将在本地存储中进行更新。

Currently, it is working on all browsers, but not, what a surprise, with IE8 and below. 目前,它可以在所有浏览器上使用,但对于IE8及更低版本,它并不令人惊讶。 IE returns NaN . IE返回NaN

Could you advice me how to store date and compare it with current time - 5 minutes to be cross-browser? 您能否建议我如何存储日期并将其与当前时间进行比较-需要5分钟才能跨浏览器浏览? Maybe with miliseconds, or some time format that is recognized with all browsers? 也许以毫秒为单位,或者是所有浏览器都可以识别的某种时间格式?

Here is the code: 这是代码:

$(document).ready(function() {

  var side_user_cp = $.jStorage.get("side_user_cp");
  var latest_update = new Date($.jStorage.get("latest_update")); // Here is where I get NaN with IE
  var now_date = new Date();

  if(!side_user_cp || !latest_update){ // If browser doesn't support local storage, or it first time visit, load it with AJAX
    $.get('/ajax/side_user_cp/', function(data) {
            $.jStorage.set("latest_update",now_date);
            $.jStorage.set("side_user_cp",data);
            $('#side').prepend(data);
          });
  }
  else // Browser has support, so check should it be loaded with with AJAX or from Local storage
  {
    var check_date = new Date(now_date);
    check_date.setMinutes(check_date.getMinutes()-5);

    if(check_date > latest_update) // latest_update from Loacal storage is here NaN
    {
      $.get('/ajax/side_user_cp/', function(data) {
            $.jStorage.set("latest_update",now_date);
            $.jStorage.set("side_user_cp",data);
            $('#side').prepend(data);
       });
    }
    else
    {
      $('#side').prepend(side_user_cp);
    }
});

You're storing a "Date" instance , and then passing it back in to the Date constructor. 您要存储一个“ Date” 实例 ,然后将其传递回Date构造函数。 That's probably not what you want to do. 那可能不是您想要的。

You could store the raw numeric timestamp: 您可以存储原始数字时间戳记:

    $.jStorage.set("latest_update", now_date.getTime());

Then it'd make sense to pass that to construct a new Date later. 然后将其传递给以后构造一个新的Date是有意义的。

(Note that this is a guess; I'm trying to figure out whether IE8 will be confused as you describe if the Date constructor is passed a Date, but jsfiddle appears to have issues at the moment :/ ) (请注意,这只是个猜测;我正在尝试弄清IE8是否会如您所描述的那样混淆,如果Date构造函数传递了Date,但是jsfiddle目前似乎有问题:/)

editnope - bogus. 编辑 - 不-虚假。 ... edit again but apparently this was helpful (??). ... 再次编辑,但显然这很有帮助(??)。 When I said it was "bogus", what I meant was that IE seemed to be OK with instantiating a Date from another Date. 当我说这是“伪造的”时,我的意思是,从另一个Date实例化一个Date似乎IE可以。 Possibly however the issue was that Date objects can't be successfully stored in local storage? 但是,问题可能是Date对象无法成功存储在本地存储中?

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

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