3 回答

TA貢獻2011條經驗 獲得超2個贊
以下代碼片段假定日期是字符串。
對于其中任何一個是Date對象的,var dx=new Date(String)都可以跳過。
var str1="2020-09-20T16:26:38.000Z";
var str2="Sun Sep 20 2020 00:00:00 GMT-0400 (Eastern Daylight Time)";
var d1=new Date(str1);
var d2=new Date(str2);
console.log("str1="+str1);
console.log("str2="+str2);
console.log("d1="+d1);
console.log("d2="+d2);
console.log("d1.toDateString()==d2.toDateString() is "+(d1.toDateString()==d2.toDateString()));
.as-console-wrapper { max-height: 100% !important; top: 0; }

TA貢獻1790條經驗 獲得超9個贊
剛剛新的日期
使用Date.getMonth()或Date.getFullYear()
const a=new Date("Sun Sep 20 2020 00:00:00 GMT-0400 (Eastern Daylight Time)")
console.log(a)
//2020-09-20T04:00:00.000Z//result a
const b=new Date("2020-09-20T16:26:38.000Z")
console.log(b)
//2020-09-20T16:26:38.000Z// result b
if (a.getMonth()===b.getMonth())console.log("match")
else console.log("not match")

TA貢獻2037條經驗 獲得超6個贊
您可以將它們都轉換為 Date 對象(以便獲得相同的格式),然后將它們轉換回字符串。由于前 15 個字符現在都是日期,因此您可以對它們進行子字符串化并進行比較。
編輯:你不必真正轉換第二個,因為它已經是正確的格式,但為了徹底起見,我做了
let d1 = new Date('2020-09-20T16:26:38.000Z').toString().substring(0, 15),
d2 = new Date('Sun Sep 20 2020 00:00:00 GMT-0400').toString().substring(0, 15);
console.log(d1 === d2); //true
添加回答
舉報