用javascript计算上一个结束日期和下一个开始日期之间的间隔
Calculating the gap between the previous end date and the next start date in javascript
编辑:我在下面的答案中尝试了一些建议,但仍然没有得到合适的结果。如有任何进一步的帮助,我们将不胜感激。
我已经用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
进行比较。这就是你要找的吗?
您可以执行类似于检查五年范围的操作:首先确保startDate
和endDate
不为空,然后比较startDate
(startDate[0]
)中的第一个日期和endDate
(endDate[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函数。
- 希望日期开始结束于while循环中的一个房间id的一个数组
- jQuery Datepicker从日期开始设置默认值
- 高图表日期时间从错误的日期开始
- Javascript - 从当前日期开始的上个月计算
- 正在加载动态日历以在一个月的正确日期开始
- 如何在Moment.js中从日期开始获取一个月中的星期
- javascript从给定日期开始按月递增日期
- 从单击日期开始将数据放入输入类型中
- 用于调查的 Javascript:从页面加载时从出生日期开始计算年龄 从预填充信息加载
- 日期选取器日期范围:最大日期的最小日期 = 开始日期 + 1 天
- 谷歌时间线图表从错误的日期开始
- jquery从格式01/02/2010的日期开始修剪前导零
- Javascript从日期开始获取星期几
- 如何使用Javascript AngularJS从日期选择器中的当前日期开始
- 如何将日期时间分配给具有间隔和日期开始的高图
- 使用flipclock.js从一个特定的日期开始倒数
- 在代码编写器中验证开始和结束日期.开始日期小于结束日期
- 在ng-repeat Angularjs中显示从日期开始的时间
- 从日期开始增加变量Javascript
- 如何选择从开始日期到结束日期(即不包括结束日期)开始的序列化数据