JQuery / JS - How to compare two date / time stamps?

I have two date / time stamps:

d1 = 2011-03-02T15:30:18-08:00 
d2 = 2011-03-02T15:36:05-08:00

I want to be higher to compare two:

if (new Date(d1) < new Date(d2)) {alert('newer')}

But this does not work correctly. Is there a way to compare not only dates but also time? thank

UPDATE:

console.log(d1 + ' ' + d2);
console.log(new Date(d1) > new Date(d2))


2011-03-02T15:30:18-08:00 2011-03-02T15:36:05-08:00
false
2011-03-02T15:30:18-08:00 2011-03-02T15:30:18-08:00
false
2011-03-02T15:30:18-08:00 2011-03-02T14:15:04-08:00
false
+3
source share
5 answers

Your timestamps should be strings.

var d1 = "2011-03-02T15:30:18-08:00";
var d2 = "2011-03-02T15:36:05-08:00";

if (new Date(d1) < new Date(d2)) {alert('newer')}

Example: http://jsfiddle.net/hKPkF/

+12
source

You may have problems with the date string format. I get an invalid date if:

new Date("2011-03-02T15:30:18-08:00");

Here's what works for me in Chrome:

var d1 = "Thu Mar 03 2011 00:53:54 GMT+0100 (CET)";
var d2 = "Thu Mar 03 2011 03:53:54 GMT+0100 (CET)";

if (new Date(d1) < new Date(d2)) {console.log('newer')}

If you work in ruby ​​from the server side, you can convert to UTC from the object Time. Here, with a little massage to convert to a format that is identical to the javascript Date object method toUTCString:

tm = Time.new
utc_tm = tm.getutc
utc_tm.strftime("%a, %d %b %Y %H:%M:%S GMT")

: "Thu, 03 Mar 2011 00:46:55 GMT"

+4

, , ( , ):

var d1 = "2011-03-02T15:30:18-08:00";
var d2 = "2011-03-02T15:36:05-08:00";

if(new Date(d1) < new Date(d2)) {alert('newer')};

, . :

alert(new Date(d1) - new Date(d2));

347000, 347 , 5 , 47 . .

+2

var d1 = '2011-03-02T15: 30: 18-08: 00', d2 = '2011-03-02T15: 36: 05-08: 00'; ISO Date, Date Date.parse.

, , - .

, , , . , array.map, mozilla org.

Date.fromISO= function(s){
    var day, tz, 
    rx=  /^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d\d):(\d\d))?$/, 
    p= rx.exec(s) || [];
    if(p[1]){
        day= p[1].split(/\D/).map(function(itm){
            return parseInt(itm, 10) || 0;
        });
        day[1]-= 1;
        day= new Date(Date.UTC.apply(Date, day));
        if(!day.getDate()) return NaN;
        if(p[5]){
            tz= parseInt(p[5], 10)*60;
            if(p[6]) tz += parseInt(p[6], 10);
            if(p[4]== "+") tz*= -1;
            if(tz) day.setUTCMinutes(day.getUTCMinutes()+ tz);
        }
        return day;
    }
    return NaN;
}
Array.prototype.map= Array.prototype.map || function(fun, scope){
    var L= this.length, A= [], i= 0;
    if(typeof fun== 'function'){
        while(i< L){
            if(i in this) A[i]= fun.call(scope, this[i], i, this);
            ++i;
        }
        return A;
    }
}
var d1= '2011-03-02T15:30:18-08:00', d2= '2011-03-02T15:36:05-08:00';
alert(Date.fromISO(d1)-Date.fromISO(d2)+' milliseconds')
+1

Given the differences in date formats and capabilities between browsers, I would really recommend using a library dedicated to handling DateTime. JS support for him is known to be terrifying. I am a HUGE fan on date.js

http://www.datejs.com/

+1
source

Source: https://habr.com/ru/post/1795894/


All Articles