检查日期是 "tomorrow" 还是 "the day after tomorrow"
Check if a date is "tomorrow" or "the day after tomorrow"
我正在寻找获取“明天”和“后天”的方法 returned来自接收日期的函数:
当前日期:
"2015/04/24 18:15:00"
未来日期:
"2015/04/25 02:40:00"
这里的函数应该return"tomorrow"。我尝试查找一些函数,但它们都是 return 0 而不是 1.
function days_between(date1, date2) {
// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24
// Convert both dates to milliseconds
var date1_ms = date1.getTime()
var date2_ms = date2.getTime()
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)
// Convert back to days and return
return Math.round(difference_ms/ONE_DAY)
}
有什么想法吗?
首先,这是 Javascript 日期 object and its prototype 的文档。
那么,如何确定某一天 (date2
) 是否是一天又一天 (date1
)?那么,将一天添加到 date1
并查看日期是否匹配:
var date1_tomorrow = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate() + 1);
if (date1_tomorrow.getFullYear() == date2.getFullYear() && date1_tomorrow.getMonth() == date2.getMonth() && date1_tomorrow.getDate() == date2.getDate()) {
return "tomorrow"; // date2 is one day after date1.
}
如果要确定 date2
是否比 date1
晚两天,可以使用与上述相同的逻辑,但添加 2 天而不是 1 天:
var date1_overmorrow = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate() + 2);
if (date1_overmorrow.getFullYear() == date2.getFullYear() && date1_overmorrow.getMonth() == date2.getMonth() && date1_overmorrow.getDate() == date2.getDate()) {
return "the day after tomorrow"; // date2 is two days after date1.
}
如果日期不是相隔 1 天或 2 天,最后一位将只是 return 所需格式的日期:
return date2.toLocaleString(); // Firefox 29+, Chrome 24+, IE 11+
// OR
return date2.getFullYear() + '/' + (date2.getMonth() + 1) + '/' + date2.getDate() + ' ' + date2.getHours() + ':' + date2.getMinutes() + ':' + date2.getSeconds(); // Et al.
测试
function incrementDate(date, n) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + n);
}
function compareDates(date1, date2) {
return date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate();
}
function formatDate(date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
return year + '-' + (month < 10 ? '0' : '') + month + '-' + (day < 10 ? '0' : '') + day;
}
$(function() {
var $output = $('#output');
var comparisons = [
{'start': new Date(2016, 6 - 1, 1), 'inc': 1, 'target': new Date(2016, 6 - 1, 2)},
{'start': new Date(2016, 6 - 1, 30), 'inc': 1, 'target': new Date(2016, 7 - 1, 1)},
{'start': new Date(2016, 12 - 1, 31), 'inc': 1, 'target': new Date(2017, 1 - 1, 1)}
];
for (var i = 0, len = comparisons.length; i < len; i += 1) {
var comp = comparisons[i];
var $row = $('<tr>');
$('<td>').text(formatDate(comp.start)).appendTo($row);
$('<td>').text(comp.inc).appendTo($row);
$('<td>').text(formatDate(comp.target)).appendTo($row);
$('<td>').text(compareDates(comp.target, incrementDate(comp.start, comp.inc)) ? 'yes' : 'no').appendTo($row);
$output.append($row);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Start</th>
<th>Inc</th>
<th>Target</th>
<th>Equal</th>
</tr>
</thead>
<tbody id="output">
</tbody>
</table>
我会创建两个日期对象。一个代表今天的午夜,另一个代表明天的午夜。如果我比较的日期在这两个日期之间,那么我知道它属于“明天”的范畴。如果我想提前两天做,想法是一样的,但日期对象将代表未来的额外日期。
例如:
const oneDay = 1000 * 60 * 60 * 24;
function getMidnight(day){
const date = new Date(day);
date.setMilliseconds(999);
date.setSeconds(59);
date.setMinutes(59);
date.setHours(23);
return date;
}
function isTomorrow(date){
const midnightTonight = getMidnight(new Date());
const midnightTomorrow = new Date(midnightTonight.getTime() + oneDay);
return date > midnightTonight && date < midnightTomorrow;
}
function isDayAfterTomorrow(date){
const midnightTomorrow = getMidnight(new Date(Date.now() + oneDay));
const midnightDayAfterTomorrow = new Date(midnightTomorrow + oneDay);
return date > midnightTomorrow && date < midnightDayAfterTomorrow;
}
我正在寻找获取“明天”和“后天”的方法 returned来自接收日期的函数:
当前日期:
"2015/04/24 18:15:00"
未来日期:
"2015/04/25 02:40:00"
这里的函数应该return"tomorrow"。我尝试查找一些函数,但它们都是 return 0 而不是 1.
function days_between(date1, date2) {
// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24
// Convert both dates to milliseconds
var date1_ms = date1.getTime()
var date2_ms = date2.getTime()
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)
// Convert back to days and return
return Math.round(difference_ms/ONE_DAY)
}
有什么想法吗?
首先,这是 Javascript 日期 object and its prototype 的文档。
那么,如何确定某一天 (date2
) 是否是一天又一天 (date1
)?那么,将一天添加到 date1
并查看日期是否匹配:
var date1_tomorrow = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate() + 1);
if (date1_tomorrow.getFullYear() == date2.getFullYear() && date1_tomorrow.getMonth() == date2.getMonth() && date1_tomorrow.getDate() == date2.getDate()) {
return "tomorrow"; // date2 is one day after date1.
}
如果要确定 date2
是否比 date1
晚两天,可以使用与上述相同的逻辑,但添加 2 天而不是 1 天:
var date1_overmorrow = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate() + 2);
if (date1_overmorrow.getFullYear() == date2.getFullYear() && date1_overmorrow.getMonth() == date2.getMonth() && date1_overmorrow.getDate() == date2.getDate()) {
return "the day after tomorrow"; // date2 is two days after date1.
}
如果日期不是相隔 1 天或 2 天,最后一位将只是 return 所需格式的日期:
return date2.toLocaleString(); // Firefox 29+, Chrome 24+, IE 11+
// OR
return date2.getFullYear() + '/' + (date2.getMonth() + 1) + '/' + date2.getDate() + ' ' + date2.getHours() + ':' + date2.getMinutes() + ':' + date2.getSeconds(); // Et al.
测试
function incrementDate(date, n) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + n);
}
function compareDates(date1, date2) {
return date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate();
}
function formatDate(date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
return year + '-' + (month < 10 ? '0' : '') + month + '-' + (day < 10 ? '0' : '') + day;
}
$(function() {
var $output = $('#output');
var comparisons = [
{'start': new Date(2016, 6 - 1, 1), 'inc': 1, 'target': new Date(2016, 6 - 1, 2)},
{'start': new Date(2016, 6 - 1, 30), 'inc': 1, 'target': new Date(2016, 7 - 1, 1)},
{'start': new Date(2016, 12 - 1, 31), 'inc': 1, 'target': new Date(2017, 1 - 1, 1)}
];
for (var i = 0, len = comparisons.length; i < len; i += 1) {
var comp = comparisons[i];
var $row = $('<tr>');
$('<td>').text(formatDate(comp.start)).appendTo($row);
$('<td>').text(comp.inc).appendTo($row);
$('<td>').text(formatDate(comp.target)).appendTo($row);
$('<td>').text(compareDates(comp.target, incrementDate(comp.start, comp.inc)) ? 'yes' : 'no').appendTo($row);
$output.append($row);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Start</th>
<th>Inc</th>
<th>Target</th>
<th>Equal</th>
</tr>
</thead>
<tbody id="output">
</tbody>
</table>
我会创建两个日期对象。一个代表今天的午夜,另一个代表明天的午夜。如果我比较的日期在这两个日期之间,那么我知道它属于“明天”的范畴。如果我想提前两天做,想法是一样的,但日期对象将代表未来的额外日期。
例如:
const oneDay = 1000 * 60 * 60 * 24;
function getMidnight(day){
const date = new Date(day);
date.setMilliseconds(999);
date.setSeconds(59);
date.setMinutes(59);
date.setHours(23);
return date;
}
function isTomorrow(date){
const midnightTonight = getMidnight(new Date());
const midnightTomorrow = new Date(midnightTonight.getTime() + oneDay);
return date > midnightTonight && date < midnightTomorrow;
}
function isDayAfterTomorrow(date){
const midnightTomorrow = getMidnight(new Date(Date.now() + oneDay));
const midnightDayAfterTomorrow = new Date(midnightTomorrow + oneDay);
return date > midnightTomorrow && date < midnightDayAfterTomorrow;
}