Mario Parra
Mario Parra

Reputation: 1554

If statement with date comparison

I'm trying to write an if statement that runs some code if the date is after April 24th, 2017, 10 am EDT, but it doesn't appear that my variables are comparable (different data types?).

I'm trying to avoid using Moment.js for just this.

var today = new Date();
var launch = 'Mon Apr 24 2017 10:00:00 GMT-0400 (EDT)';

today returns Tue Apr 04 2017 14:34:41 GMT-0400 (EDT).

When I test if either is greater than the other, both are false. How should I format my dates?

Thanks!

Upvotes: 0

Views: 50

Answers (1)

Avitus
Avitus

Reputation: 15958

You have to have launch as a date type:

var today = new Date();
var launch = 'Mon Apr 24 2017 10:00:00 GMT-0400 (EDT)';
var launchDate = Date.parse(launch);

if ( launchDate > today ) 

You should also read more about dates here: Compare two dates with JavaScript

Upvotes: 3

Related Questions