比较JavaScript中的两个日期 [英] Compare two dates in JavaScript

查看:78
本文介绍了比较JavaScript中的两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个日期。我有这个代码,我认为这将是一种享受,但事实并非如此。现在我只想在结束日期小于开始日期时发出错误警告。日期样式 yyyy-mm-dd 需要以此格式保存,以用于此之前的其他事件。这段代码有什么问题?

I am trying to compare two dates. I have this code which I thought would work a treat, but it didn't. For now I just want to alert with an error if the end date is less than the start date. The date style, yyyy-mm-dd, needs to be kept in this format for other events prior to this. What is wrong with this code?

   startdate = "2009-11-01" ;
   enddate  = "2009-11-04" ;

   var d1 = new Date(startdate)
   var d2 = new Date(enddate)

   if (d2 < d1) {
      alert ("Error ! ) ;
   }

   document.cookie='st =' + startdate // set sytem cookie
   document.cookie='en =' + enddate
   window.location = self.location.href
   window.opener.location.reload()
   close()


推荐答案

有人最终使用ISO 8601标准日期,但随后......



您使用的是JavaScript可以理解的很好的国际标准。但它没有。

Someone finally uses ISO 8601 standard dates but then ...

You are using a nice international standard that JavaScript arguably should understand. But it doesn't.

问题是你的日期在 ISO 8601 标准格式,内置 Date.parse()无法读取。

The problem is that your dates are in ISO 8601 standard format which the built-in Date.parse() can't read.

JavaScript通过 RFC 822实现较旧的IETF日期 / 1123.就是这样lution是将它们调整为RFC风格,你可以在RFC1123中看到 ,看起来像 dd月yyyy

JavaScript implements the older IETF dates from RFC 822/1123. One solution is to tweak them into the RFC-style, which you can see in RFC1123, and which look like dd month yyyy.

有关于可以全面扫描ISO格式的编码浮动,现在您知道了谷歌为iso标准日期你可以得到它。 在这里我发现了这个:

There is coding floating about that can scan the ISO format comprehensively, and now that you know to google for "iso standard date" you can get it. Over here I found this:

Date.prototype.setISO8601 = function (string) {
    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
        "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
        "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));

    var offset = 0;
    var date = new Date(d[1], 0, 1);

    if (d[3]) { date.setMonth(d[3] - 1); }
    if (d[5]) { date.setDate(d[5]); }
    if (d[7]) { date.setHours(d[7]); }
    if (d[8]) { date.setMinutes(d[8]); }
    if (d[10]) { date.setSeconds(d[10]); }
    if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
    if (d[14]) {
        offset = (Number(d[16]) * 60) + Number(d[17]);
        offset *= ((d[15] == '-') ? 1 : -1);
    }

    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    this.setTime(Number(time));
}

js> t = new Date()
Sun Nov 01 2009 09:48:41 GMT-0800 (PST)
js> t.setISO8601("2009-11-01")
js> t

Sat Oct 31 2009 17:00:00 GMT-0700 (PDT)

11-01在我的时区重新解释,只要您的所有日期获得相同的转换,然后他们应该合理地进行比较,否则您可以将TZ信息添加到您的字符串或Date对象。

The 11-01 is reinterpreted in my timezone, as long as all your dates get the same conversion then they should compare reasonably, otherwise you can add TZ info to your string or to the Date object.

这篇关于比较JavaScript中的两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆