unixTimeStamp1 = 1532676600;
unixTimeStamp2 = 1532680500;
我必须比较以上两个unix时间戳值,如果两个日期相同,则返回true。
答案 0 :(得分:4)
比较每个日期的toDateString
:
const getDateStr = secs => {
const d = new Date(secs * 1000);
return d.toDateString();
};
const d1 = getDateStr(1532676600);
const d2 = getDateStr(1532680500);
console.log(d1);
console.log(d2);
console.log(d1 === d2);
答案 1 :(得分:1)
您需要使用new Date().toDateString()
从该时间戳获取完整日期值。然后比较一下。这也是一个unix时间戳,因此您需要先使用unixTimeStamp * 1000
var unixTimeStamp1 = 1532676600;
var unixTimeStamp2 = 1532680500;
function checkDate(unixTimeStamp1, unixTimeStamp2){
var date1 = new Date(unixTimeStamp1 * 1000).toDateString();
var date2 = new Date(unixTimeStamp2 * 1000).toDateString();
return date1 === date2;
}
console.log(checkDate(unixTimeStamp1, unixTimeStamp2));
unixTimeStamp1 = 1532156518;
unixTimeStamp2 = 1533839400;
console.log(checkDate(unixTimeStamp1, unixTimeStamp2));
但是,如果您只想获取忽略年份和月份的日期,则可以使用getDate()
:
var unixTimeStamp1 = 1532676600;
var unixTimeStamp2 = 1532680500;
function checkDate(unixTimeStamp1, unixTimeStamp2){
var date1 = new Date(unixTimeStamp1 * 1000).getDate();
var date2 = new Date(unixTimeStamp2 * 1000).getDate();
return date1 === date2;
}
console.log(checkDate(unixTimeStamp1, unixTimeStamp2));
unixTimeStamp1 = 1532156518;
unixTimeStamp2 = 1533839400;
console.log(checkDate(unixTimeStamp1, unixTimeStamp2));
答案 2 :(得分:1)
var unixTimeStamp1 = 1532676600;
var unixTimeStamp2 = 1532680500;
var date1 = new Date(unixTimeStamp1 * 1000);
var date2 = new Date(unixTimeStamp2 * 1000);
if ((date1.getFullYear() === date2.getFullYear()) &&
(date1.getMonth() === date2.getMonth()) &&
(date1.getDate() === date2.getDate())
) {
console.log("same");
}