我需要一些JavaScript代码帮助,如果第二个日期大于3天,我需要检查两个字符串日期值(firstDate和secondDate),我正在以字符串格式mm / dd / yyyy获取这些值。请帮忙。
var firstDate = '10/01/2019'
var secondDate = '10/04/2019'
if ((inputData.firstDate)+3 === inputData.secondDate) {
//return []; //
return {dateCheck: 'Not greater than 3 days'};
}
else
{
return {dateCheck: 'Greater than 3 days'};
}
答案 0 :(得分:2)
要将N
天添加到您使用的日期Date.getDate() + N
var d1 = new Date('10/01/2019');
var d2 = new Date('10/04/2019');
var isGreater = +d2 > d1.setDate(d1.getDate() + 3); // false (equals three days)
鉴于以上所述,您可以创建一个简单的可重用功能
/**
* Check if d2 is greater than d1
* @param {String|Object} d1 Datestring or Date object
* @param {String|Object} d2 Datestring or Date object
* @param {Number} days Optional number of days to add to d1
*/
function isDateGreater (d1, d2, days) {
d1 = new Date(d1);
return +new Date(d2) > d1.setDate(d1.getDate() + (days||0))
}
console.log(isDateGreater('10/01/2019', '10/03/2019', 3)); // false (smaller)
console.log(isDateGreater('10/01/2019', '10/04/2019', 3)); // false (equal)
console.log(isDateGreater('10/01/2019', '10/05/2019', 3)); // true (greater than)
// Without the optional third parameter
console.log(isDateGreater('10/01/2019', '10/05/2019')); // true (is greater date)
回顾一下:创建一个函数,在将N
天添加到日期后,计算两个时间戳。
答案 1 :(得分:0)
也许您可以定义一个辅助函数来解析您输入的日期字符串,以便您可以像下面这样对3天的偏移量进行每个日期之间的比较:
var firstDate = '10/01/2019'
var secondDate = '10/04/2019'
/*
Define helper function to parse input date string and
return the date as milliseconds since epoch time, whihch
gives us an easy format to compare offset dates
*/
function parseDate(str) {
/*
Break input string into date parts
*/
const parts = str.split('/').map(p => parseInt(p));
/*
Create date object and populate parts
*/
const date = new Date();
date.setTime(0);
date.setDate(parts[0]);
date.setMonth(parts[1] - 1);
date.setFullYear(parts[2]);
/*
Return date in milliseconds
*/
return date.getTime()
}
/*
If first input date + 3 days (in milliseconds) is less than second input
date, then second date is not greater than 3 days since first date
*/
if (parseDate(firstDate) + (86400000 * 3) < parseDate(secondDate)) {
console.log('Not greater than 3 days');
} else {
console.log('Greater than 3 days');
}
答案 2 :(得分:0)
我已将其包含在函数中,以便您进行测试。
在这里我们正在检查((date1-date2)<259200000)(Unix时间戳中的3天)
function isItGreaterThan(date1, date2) {
var firstDate = new Date(date1);
var secondDate = new Date(date2);
if ((secondDate.getTime() - firstDate.getTime()) < 259200000) {
console.log({dateCheck: 'Not greater than 3 days'});
} else {
console.log({dateCheck: 'Greater than 3 days'});
}
}
isItGreaterThan('10/01/2019', '10/02/2019');
isItGreaterThan('10/01/2019', '10/03/2019');
isItGreaterThan('10/01/2019', '10/04/2019');
答案 3 :(得分:0)
从第二个日期起减去3天,并检查是否仍大于第一个日期。
var firstDate = '10/01/2019';
var secondDate = '10/04/2019';
var thirdDate = '11/01/2019'; // this is to test.
const pastThreeDays = (firstDate, secondDate) => {
// Time is measured in milliseconds, so lets create a variable for a day.
const day = 1000 * 60 * 60 * 24;
let fDate = new Date(firstDate);
let sDate = new Date(secondDate);
sDate = new Date(sDate.getTime() - (3 * day));
console.log(`${fDate} - ${sDate}`);
// If sDate is still greater than fDate after subtracting 3 days then we are past three days.
return fDate.getDate() < sDate.getDate();
};
console.log(pastThreeDays(firstDate, secondDate));
console.log(pastThreeDays(firstDate, thirdDate));
答案 4 :(得分:0)
对于许多不同的Date
用法,我建议使用dateFns
。 DateFns
是日期对象JS库,具有许多date
操作可用的方法。
参考:DateFns
您还可以使用库中的format
以不同的格式设置日期
为此,我建议使用addDays
中的dateFns
方法
const secondDate = '04/04/2019',
firstDate = '04/01/2019'
alert(new Date(secondDate) >= dateFns.addDays(new Date(firstDate), 3))
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.min.js"></script>