[英]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.我在
EEE MMM dd hh:mm:ss TMZ yyyy
格式中得到两个字符串(日期),我想在 Javascript/Jquery 中比较这两个日期。
Example strings : Fri Aug 14 13:12:45 CDT 2020, Tue Aug 25 05:33:19 CDT 2020
示例字符串:
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.您需要将日期解析为 Javascript 日期对象。 With the format you're mentioning a simple
new Date(longstringdateformat)
would work.使用您提到的格式,一个简单的
new Date(longstringdateformat)
将起作用。 Then you can compare the value in milliseconds from the getTime()
method.然后您可以比较
getTime()
方法中的值(以毫秒为单位getTime()
。
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.这里如果返回值是负数 date2 大于 date1,如果它是正数 date2 小于 date1 并且如果它是 0 它们是相等的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.