Reputation: 391
How can compare two variables in JS with value as time string in the below format
Mon, 29 Feb 2016 09:09:53 GMT
Thu, 21 Jan 2016 05:08:46 GMT
Simply doing x > y will work ??? Or Do I need to convert into other format..
strtotime()
above function is available in php which will parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC).
Is there any function to do the same in JS ??
Upvotes: 0
Views: 4538
Reputation: 391
I got it.
getTime() returns the number of milliseconds since January 1, 1970:
<script>
var d = new Date();
var time = d.getTime();
</script>
Upvotes: 1
Reputation: 673
In Javascript, you should compare Date objects:
var foo = new Date("Mon, 29 Feb 2016 09:09:53 GMT");
var bar = new Date("Thu, 21 Jan 2016 05:08:46 GMT");
if(foo > bar) { ... }
There are multiple ways of creating an instance of the Date object. You can read more about it on MDN.
Storing the time string in a variable:
var foo = "Mon, 29 Feb 2016 09:09:53 GMT";
var bar = new Date(foo);
Upvotes: 1
Reputation: 101
Date.parse('Mon, 29 Feb 2016 09:09:53 GMT') > Date.parse('Thu, 21 Jan 2016 05:08:46 GMT')
true
Upvotes: 1