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!

1
  • Just use Date.now() Commented Jul 1, 2015 at 22:20

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

1 Comment

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).
1

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

2 Comments

+date is shorter, date.getTime() is probably more semantic, but date < 1435269960 is sufficient. ;-)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.