用javascript计算上一个结束日期和下一个开始日期之间的间隔

Calculating the gap between the previous end date and the next start date in javascript

本文关键字:日期 开始 之间 下一个 javascript 计算 上一个 结束      更新时间:2023-09-26

编辑:我在下面的答案中尝试了一些建议,但仍然没有得到合适的结果。如有任何进一步的帮助,我们将不胜感激。

我已经用HTML实现了一个表单,它以2个日期作为输入,并将它们附加到两个数组中。然后我计算两个日期之间的差异(以月为单位)。

a) 该表格询问用户他/她的五年历史,因此用户应该能够输入他/她致富五年(60个月)的日期。对于这个问题,我尝试了以下功能:

if (typeof startArray[index] !== 'null' && endArray[index] !== null) {
var result = difference_between(dateFrom, dateTo);
document.getElementById("txtResult").value = result
if(result <= 60) {
document.getElementById('datepicker1').reset();
document.getElementById('datepicker2').reset();
document.getElementById('txtResult').reset();
}
else{
alert("Go to the next section");
document.getElementById('btnSave').disable = true;
}

b) 表格应能够计算结束日期和下一个开始日期之间的间隔,并且间隔不应超过两个月。例如,表格应该能够将我结束学业的日期与开始上大学的日期进行比较,如果超过2个月,那么我应该显示一条错误消息。这是我的功能:

function GetDifference() {
var dateFrom = new Date(document.getElementById("datepicker1").value);
var dateTo = new Date(document.getElementById("datepicker2").value);
document.getElementById("txtResult").value = difference_between(dateFrom, dateTo);
//Is there a gap of more than 2 months?
if (endDate.length && (difference_between(endDate[endDate.length - 1], dateFrom)) > 2) {
    window.alert("There should not be a gap of more than two months in this section.");
}
}

这是JSFIDDLE:http://jsfiddle.net/stelios13/KsuxV/111/

谢谢。

startDate.splice(0,1)删除startDate中的第一个元素,我有一种感觉,这不是你想要的。我会这样检查:

function GetDifference() {
    var dateFrom = new Date(document.getElementById("datepicker1").value);
    var dateTo = new Date(document.getElementById("datepicker2").value);
    document.getElementById("txtResult").value = difference_between(dateFrom, dateTo);
    // Right now the things in endDate are strings so this won't work, but see below
    if(endDate.length && (difference_between(endDate[endDate.length - 1], dateFrom)) > 2){
        window.alert("There should not be a gap of more than two months in this section. Plsease fill in your details appropriately");
        document.getElementById("btnSave").disabled = true; 
    }
}

…首先我确保endDate不是空的,然后我在endDate中获得最后一个日期,并将其与dateFrom进行比较。这就是你要找的吗?

您可以执行类似于检查五年范围的操作:首先确保startDateendDate不为空,然后比较startDatestartDate[0])中的第一个日期和endDateendDate[endDate.length - 1])中的最后一个日期。


顺便说一句,在JavaScript中,日期可以转换为数字(从1970年1月1日开始的任意起始点的毫秒数):

// The unary `+` operator converts its operand to a number:
console.log(+new Date()); // Just now, logged 1377880989982

这非常适合比较它们:

// How many milliseconds in a month?
var month = 1000 * 60 * 60 * 24 * 365.25 / 12;
// Subtraction also converts its operands to numbers:
console.log(
    Math.round((new Date('2013-06-01') - new Date('2013-01-01')) / month)
); // Logs 4.96

因此,从1月1日到6月1日大约有五个月。在你的情况下,如果你想更人性化一点(并确保月数相差不超过两个),那么你比较它们的方式完全可以。


另一个小建议是:如果稍后要比较这些日期,您可能希望将Date存储在数组中,而不是字符串中。而且,.push()是一种比分配给arr[arr.length]更常见的向数组添加内容的方法:

// I would change the beginning of insert() to something like this:
function insert() {
    var newStartDate = new Date(document.getElementById('datepicker1').value);
    var newEndDate = new Date(document.getElementById('datepicker2').value);
    startDate.push(newStartDate);
    endDate.push(newEndDate);
    // …
}

也许moment.js对您有用。这是一个用于处理日期的js库,它还有一个diff函数。