0

I was trying to compare two dates and time

se_time = 2017/05/16 17:41:47
curr_date = 2017/5/16 12:42:6

The result of "console.log(curr_date < se_time)" is false should not it be true?

2
  • 4
    That is invalid syntax. Commented May 16, 2017 at 16:47
  • Your dates when formatted as strings using the same date format would normally return the correct value because alphabetically they would have the same result. However, they aren't the same date format. In one you have numbers with leading zeroes and in the other you don't. You have the month in one as 05 and the other just 5 which would make them alphabetically incorrect as the sixth character 0 < 5. You would also need to correct the seconds in the time. Commented May 16, 2017 at 16:56

1 Answer 1

1

You have to create the date object first, you cannot compare the strings as they are.

var date1 = new Date('2011-04-12T10:20:30Z');
var date2 = new Date('2011-04-11T10:20:30Z');
console.log(date1>date2);//true
Sign up to request clarification or add additional context in comments.

3 Comments

How do I get the current date in "2011-04-11T10:20:30Z" format? Is there a build in function/ method for that?
You can still use the format you listed. I've updated this answer to reflect that.
No, don't do that. ECMA-262 only requires ISO 8601 extended format to be parsed, any other format is implementation dependent and may be incorrectly parsed or return an invalid Date. Write a small parsing function (maybe 3 or 4 lines of code) or use a library. See Why does Date.parse give incorrect results?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.