Reputation: 185
What's the best way to compare a Java date with Javascript date for greater than or less than comparison?
Java date format: 1985-01-01T00:00:00.000-06:00
Javascript date format: Tue Jan 29 1980 00:00:00 GMT-0600
Upvotes: 1
Views: 435
Reputation: 16718
Both Java and Javascript Date
objects have a getTime
method, that returns the number of milliseconds since the epoch. Ideally, you'd send the date from Java in that format rather than as a string.
Failing that, you could parse it in Javascript with something like this to convert it to a Date
, and then compare to the other as normal (modern browsers will be able to handle ISO 8601 out of the box, but notably IE 8 or lower won't).
Upvotes: 3
Reputation: 147413
If you have two date objects, you can compare them directly:
var date0 = new Date(2012,0,1); // 1 Jan 2012
var date1 = new Date(2012,1,1); // 1 Feb 2012
if (date0 < date1) { /* true */ }
Otherwise, if both dates are ISO8601 dates in the same time zone, you can compare them as strings:
var date0 = '2012-01-01T00:00:00.000';
var date1 = '2012-02-01T00:00:00.000';
if (date0 < date1) { /* true */ }
Upvotes: 2