Search code examples
javascriptexpressionengine

How to compare 2 dates in different format (javascript)


I am using expression engine and I put my date field in a javascript variable, it parses like this :

1435269960

So I want to check if this date is earlier than today or not. But when I create a date object and I do console.log(date) it shows me this kind of date :

Wed Jul 01 2015 18:14:33 GMT-0400 (Eastern Daylight Time)

How to change this format to the first one?

Thx!


Solution

  • You could do it like so:

    var dateNumber = 1435269960;
    var convertedDate = new Date(1000*dateNumber);
    var today = new Date();
    
    if(convertedDate < today)
        $(".date").html("The date is in the past<br/><br/>" + convertedDate + "<br/>vs<br/>" + today);
    else
    $(".date").html("The date is today or in the future<br/> (" + convertedDate + ") vs (" + today + ")");
    

    Demo: http://jsfiddle.net/db9ms49z/