Reputation: 123
I have issue when compare the two strings (both are variable meaning it could be any dates) The format of those two strings are yyyy-MM-dd.
I have two strings dates "string1" and "string2", and string1 one using date format yyyy-MM-dd and string 2 using the same format yyyy-MM-dd. What should I use if I want to compare it with the logic when string1 < string2 return string result as "X"
What I current have which not working first I try to use .substring to get the right format from string 1.
var string1 = str1.substring(0, 5) + str1.substring(5, 8) + str1.substring(8, 11);
var string2 = str2
if (string1 < string2) {
dateresult= 'X';
}
Any idea will be appreciated.
Thanks in advance.
Upvotes: 1
Views: 185
Reputation: 2536
Create Date objects and use comparison on them:
var date1 = new Date('2015-01-01'),
date2 = new Date('2015-01-01');
console.log(date1 < date2);
console.log(date1 >= date2);
Upvotes: 1
Reputation: 20399
Just construct new dates for each one, then compare them.
var x = new Date('2013-05-23');
var y = new Date('2013-05-24');
alert(x < y); //true
Upvotes: 1