Comparing MySQL Dates in JavaScript

This is just a performance test, but I did some test cases before you all press the down button =]

I just want to verify that comparing dates from a MySQL database in the format YYYY-MM-DD HH:MM:SS in JavaScript will work correctly. Unfortunately, I cannot edit the request to return a Unix timestamp.

Example:

 console.log('2010-05-22 00:54:12' > '2010-05-21 11:44:32'); 

All of my test cases seem to suggest that this works well, but I just need to make sure.

My Google-fu may be weak, but I can't find anything on the Internet. Does anyone know for sure?

+4
source share
1 answer

Yes, I'm sure one of the reasons they chose this format was because you could do a standard string comparison.

Since the order of values ​​is most significant for the least significant, this will work just fine if:

  • The hour is in 24-hour mode and
  • Any value with one digit is preceded by 0 (month, day, hour, etc.).

However, note that if you do not standardize a specific time zone (e.g. GMT), this will accurately compare dates in the same time zone.

+5
source

Source: https://habr.com/ru/post/1393374/


All Articles