简体   繁体   中英

How to compare two Date Strings in “EEE Mon dd hh:mm:ss TMZ yyyy” format in Javascript/Jquery?

I'm getting two Strings(dates) in EEE MMM dd hh:mm:ss TMZ yyyy format, I want to compare those two dates in Javascript/Jquery.

Example strings : Fri Aug 14 13:12:45 CDT 2020, Tue Aug 25 05:33:19 CDT 2020

You would need to parse the dates into a Javascript date object. With the format you're mentioning a simple new Date(longstringdateformat) would work. Then you can compare the value in milliseconds from the getTime() method.

function compareDates(){
     const date1 = new Date("Fri Aug 14 13:12:45 CDT 2020");
     const date2 = new Date("Tue Aug 25 05:33:19 CDT 2020");
     return date1.getTime()-date2.getTime();
}

here if the return value is negative date2 is greater than date1, if it's positive date2 is lesser than date1 and if it is 0 they are equal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM