[ACCEPTED]-jQuery/JS - How to compare two date/time stamps?-jquery

Accepted answer
Score: 15

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/

0

Score: 4

You may be having trouble with the date 7 string format. I am getting Invalid date 6 if I do:

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

Here's what works for me on 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 5 you are working in ruby on the server side, you 4 could convert to UTC from a Time object. Here 3 it is, with a little massaging to convert 2 to a format that is identical to javascript's 1 Date object toUTCString method:

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

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

Score: 2

Seems to work fine for me, although you 5 have to format correctly (i.e. semicolons, quotes):

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')};

And 4 yes, it takes time into account. If you 3 do this:

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

You get 347000, which is 347 seconds, or 2 5 minutes, 47 seconds. This is the correct 1 difference between the two.

Score: 1

var d1= '2011-03-02T15:30:18-08:00', d2= '2011-03-02T15:36:05-08:00'; Some 8 browsers can convert an ISO string to a 7 Date, with new Date or Date.parse.

A lot 6 of browsers in use today cannot-you may 5 need to write your own conversion.

This one 4 seems to work, but it'll need refining. I 3 added a shim for browsers that don't have 2 an array.map, based on mozilla org's public 1 code.

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')
Score: 1

Given the differences in date formats and 4 capability between browsers, I'd really 3 reccomend you use a library devoted to DateTime 2 handling. JS support for it is notoriously 1 horrendous. I'm a HUGE fan of date.js

http://www.datejs.com/

More Related questions