The Date.parse method is inconsistent in my opinion (the actual format differs from different browsers, JS engine versions, etc.). I use the following method to perform various date operations in JavaScript (e.g. comparison):
- Get the date components somehow (in your case, you can just split the string into "-").
- Build the date using the components from the previous step.
- Compare the unix timestamp of dates with the getTime () method.
Here is the code:
var d1Parts = s1.split('-'), d2Parts = s2.split('-'), d1 = new Date(d1Parts[0], d1Parts[1]-1, d1Parts[2]), d2 = new Date(d2Parts[0], d2Parts[1]-1, d2Parts[2]); if ( d1.getTime() < d2.getTime() ) { console.log('the first date is before the second'); }
source share