我想计算一个给定的日期是从今天还是昨天开始。目前我只能检查它是24小时还是48小时。我该如何查看日期?
var date_given = new Date(Date.parse(item.date));
var today = new Date();
var diff = ( Date.parse(today) - Date.parse(date_given) ) /(1000*24*60*60);
if (diff < 1) {
return "today";
} else if (diff < 2) {
return "yesterday";
}
答案 0 :(得分:2)
您可以使用这些功能添加任意天数(负数)并确定日期的日期部分是否相等:
var msInDay = 86400000;
function addDays(date, add) {
return new Date(date.valueOf() + (msInDay * add));
}
function isSameDay(a, b) {
return a.getFullYear() == b.getFullYear() &&
a.getMonth() == b.getMonth() &&
a.getDate() == b.getDate();
}
这将允许你昨天得到:
var today = new Date();
var yesterday = addDays(today, -1);
现在比较:
var otherDate = addDays(today, -1);
var isYesterday = isSameDay(otherDate, yesterday);
var isToday = isSameDay(otherDate, today);
答案 1 :(得分:2)
你可以这样做旧学校:
var dateTime = function(args) {
args = args || [0,0,0,0]
var d = new Date(Date.now())
d.setUTCHours(args[0])
d.setUTCMinutes(args[1])
d.setUTCSeconds(args[2])
d.setUTCMilliseconds(args[3])
return d;
}
var isItTodayOrYesterday = function(given) {
var start = dateTime(),
end = dateTime([23,59,59,999])
return given < start ? 'yesterday' : given < end ? 'today' : "its the futur man"
}
var futur = new Date('December 17, 2018 03:24:00'),
today = new Date(),
past = new Date('December 17, 1995 03:24:00');
console.log(isItTodayOrYesterday(past)) // yesterday
console.log(isItTodayOrYesterday(today)) // today
console.log(isItTodayOrYesterday(futur)) // its the futur man
请参阅this fiddle
修改
根据您的评论,这是一个负责yesterday
案例的版本:
var dateTime = function(args) {
var time = (args && args.time) ? args.time : [0,0,0,0],
date = new Date((args && args.date) || Date.now())
date.setUTCHours(time[0])
date.setUTCMinutes(time[1])
date.setUTCSeconds(time[2])
date.setUTCMilliseconds(time[3])
return date;
}
console.clear()
var isItTodayOrYesterday = function(given) {
var start = dateTime(),
yesterday = dateTime({date: new Date(new Date().valueOf() - 3600 * 24 * 1000)}),
end = dateTime({time: [23,59,59,999]})
return given < yesterday ? "we're stuck in the past"
: given < start ? 'yesterday'
: given < end ? 'today'
: "its the futur man"
}
var futur = new Date('December 17, 2018 03:24:00'),
today = new Date(),
past = new Date('December 17, 1995 03:24:00'),
yesterday = dateTime({
date: new Date(new Date().valueOf() - 3600 * 24 * 1000),
time: [12,21,30,0]
});
console.log(isItTodayOrYesterday(past)) // "we're stuck in the past"
console.log(isItTodayOrYesterday(yesterday)) // yesterday
console.log(isItTodayOrYesterday(today)) // today
console.log(isItTodayOrYesterday(futur)) // its the futur man
答案 2 :(得分:1)
如果您只想查看给定日期是今天还是昨天,那么只需尝试此功能。您不需要编写很长的代码:
function checkTodayYesterday(givenDate) {
var today = new Date();
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
if(today.toDateString() === givenDate.toDateString()) {
console.log('givenDate is today');
} else if(yesterday.toDateString() === givenDate.toDateString()) {
console.log('givenDate is yesterday');
} else {
console.log('givenDate is other than today or yesterday');
}
}
快乐的编码! :)