簡體   English   中英

檢查兩個日期是否具有相同的日期信息

[英]Checking if two Dates have the same date info

如何檢查兩個不同的日期對象是否具有相同的日期信息(具有相同的日期、月份、年份......)? 我試過“==”、“===”和.equals,但似乎都沒有。

您可以使用valueOf()getTime()

a = new Date(1995,11,17);
b = new Date(1995,11,17);

a.getTime() === b.getTime() // prints true

如果您只想檢查日期是否在同一天發生而不管時間如何,那么您可以使用toDateString() 方法進行比較。 此方法僅返回日期而不返回時間:

var start = new Date('2015-01-28T10:00:00Z');
var end = new Date('2015-01-28T18:00:00Z');

if (start.toDateString() === end.toDateString()) {
  // Same day - maybe different times
} else {
  // Different day
}

我使用了這個代碼:

Date.prototype.isSameDateAs = function(pDate) {
  return (
    this.getFullYear() === pDate.getFullYear() &&
    this.getMonth() === pDate.getMonth() &&
    this.getDate() === pDate.getDate()
  );
}

然后你就可以這樣稱呼它: if (aDate.isSameDateAs(otherDate)) { ... }

類型轉換為整數:

a = new Date(1995,11,17);
b = new Date(1995,11,17);
+a === +b;  //true

赫爾納爾,

您可以嘗試(請原諒函數名稱 :) - 根據 felix 的 valueof 進行修改,而不是 getTime)

function isEqual(startDate, endDate) {
    return endDate.valueOf() == startDate.valueOf();
}

用法:

if(isEqual(date1, date2)){
   // do something
}

可能會讓你參與其中。

也可以看看:

'http://www.java2s.com/Tutorial/JavaScript/0240__Date/DatevalueOf.htm'

減去它們並與零比較:

var date1 = new Date();
var date2 = new Date();

// 對日期做一些事情...

(date1 - date2) ? alert("not equal") : alert("equal");

將其放入變量中:

var datesAreSame = !(date1 - date2);

為了獲得更好的日期支持,請使用moment.jsisSame方法

var starDate = moment('2018-03-06').startOf('day');
var endDate  = moment('2018-04-06').startOf('day');

console.log(starDate.isSame(endDate)); // false ( month is different )

var starDate = moment('2018-03-06').startOf('day');
var endDate  = moment('2018-03-06').startOf('day');

console.log(starDate.isSame(endDate)); // true ( year, month and day are the same )

一個簡單的單行替代方法,用於確定兩個日期是否相等,忽略時間部分:

function isSameDate(a, b) {
    return Math.abs(a - b) < (1000 * 3600 * 24) && a.getDay() === b.getDay();
}

它確定日期 a 和 b 是否相差不超過一天並共享一周中的同一天。

 function isSameDate(a, b) { return Math.abs(a - b) < (1000 * 3600 * 24) && a.getDay() === b.getDay(); } console.log(isSameDate(new Date(2017, 7, 21), new Date(2017, 7, 21))); //exact same date => true console.log(isSameDate(new Date(2017, 7, 21, 23, 59, 59), new Date(2017, 7, 21))); //furthest same dates => true console.log(isSameDate(new Date(2017, 7, 20, 23, 59, 59), new Date(2017, 7, 21))); //nearest different dates => false console.log(isSameDate(new Date(2016, 7, 21), new Date(2017, 7, 21))); //different year => false console.log(isSameDate(new Date(2017, 8, 21), new Date(2017, 7, 21))); //different month => false

我喜歡使用這個代碼

 function isEqualDate(d1, d2) { return ( d1.toISOString().startsWith(d2.toISOString().substring(0,10)) ); } console.log(isEqualDate(new Date(2022,2,2,10,57), new Date(2022,2,2,11,57)) ) console.log(isEqualDate(new Date(), new Date(2022,2,2)) ) console.log(isEqualDate(new Date(), new Date()) )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM