1

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.

8
  • 2
    If the strings are already in the format yyyy-MM-dd then you can just do a < comparison on them directly and it should work, because even though that's a lexical string comparison, with dates in that format lexical string order is also date order. Your thing with the .substring() is pointless - it simply extracts the first 10 characters of the original string in three parts but then puts them back together in the same order (unless your original input had more characters after the date?). Commented Jul 5, 2015 at 1:44
  • The strange thing is, if I define string 1 to some fix date, it works. e.g. var string1 = '1881-05-09'; but if I using .substring to format it, it will not work. So that's why I have no idea where went wrong. Commented Jul 5, 2015 at 1:47
  • Please edit the question to show your actual inputs. (E.g., how you have assigned values to str1 and str2.) Commented Jul 5, 2015 at 1:50
  • Why do you use str1.substring(0, 5) + str1.substring(5, 8) + str1.substring(8, 11)? str1.slice(0,11) is shorter. Commented Jul 5, 2015 at 1:51
  • @nnnnnn, this is my actual code, the only difference is I change the name of the field for the question. the input came from other mapping. The mapping link I checked it do input the string into this script. Commented Jul 5, 2015 at 2:00

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Hold on. yyyy-MM-dd represents "two-alphabet month text" so it should be something like "2015-Jun-12", not "2015-06-12".
@TaoP.R. - In some languages, date formatting functions may use MM to indicate string month names like "Jun" or "Aug", but in other languages it means two-digit month like "06" or "08". Does Javascript have a date formatting function that defines what MM means in JavaScript? In any case, the OP gave a specific example in a comment: '1881-05-09'.
new Date('2015-Jan-01') would output a date the same as new Date('2015-01-01') would. JavaScript doesn't have any real, native date formatting.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.