Search code examples
javascriptdatetimedategoogle-chrome-extensiondatejs

Date.JS parse isBefore problem


Im using date.js to compare to dates that are written in user friendly string form(Sat, 1 July 2006 12:34:14). I use this code.

function is_new(lasttime, newtime){
    lasttime = Date.parse(lastime);
    newtime = Date.parse(newtime);
    if(lasttime.isBefore(newtime)){
        return true;
    }else{
        return false;
    }
}

Both lasttime and newtime are strings like above. When I try this i get

Uncaught TypeError: Object Tue Dec 30 1997 00:00:00 GMT+0000 (GMT Standard Time) has no method 'isBefore'

Solution

  • There's no function named isBefore. Instead, compare the timestamps:

    function is_new(LastTime,NewTime){
      return new Date(LastTime).getTime()<new Date(NewTime).getTime();
    }
    
    alert(is_new("Dec 30, 1997","Dec 31, 1997"));