1

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!

Matthieu Boisjoli
  • 1,057
  • 2
  • 11
  • 17

2 Answers2

1

When you want the long value for a Date object you can call the valueOf() method: date.valueOf() < 1435269960.

  • `+date` is shorter, `date.getTime()` is probably more semantic, but `date < 1435269960` is sufficient. ;-) – RobG Jul 01 '15 at 23:53
  • The valueOf method is the correct way to support integer operators as `<` and `+` for objects. Under the hood `valueOf` is called, so I guess that is the cleanest way to do it. Example: `+({ valueOf: function() { return 5; }})` returns `5`. – Jan-Willem Gmelig Meyling Jul 04 '15 at 23:43
1

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/

leemac
  • 350
  • 1
  • 5
  • 15
  • Good answers explain the issue and why the proposed solution fixes it, e.g. [*How to convert unix timestamp to JavaScript date object (consider time zone)*](http://stackoverflow.com/questions/11656601/how-to-convert-unix-timestamp-to-javascript-date-object-consider-time-zone). – RobG Jul 02 '15 at 00:02