简体   繁体   English

使用JavaScript计算日期范围

[英]Calculate date range using javascript

I use a date/time picker to select date and time: 我使用日期/时间选择器选择日期和时间:

var d = new Date();

$('#timePicker').datetimepicker({
    dateFormat: 'dd-mm-yyyy',
    timeFormat: 'hh:mm',
    separator: '@',
    minDate: new Date(),
    maxDate: new Date(),
});

minDate and maxDate define the date range. minDate和maxDate定义日期范围。 No dates in the past should be selected, so the current date is the lower bound. 不应选择过去的日期,因此当前日期是下限。

The upper bound should be max 2 month in the future. 上限应为将来的最大2个月。 So how do I handle that when current month is 11? 那么当当前月份为11时该如何处理? Any ideas on a function that calculates date range? 关于计算日期范围的函数有什么想法吗?

var d = new Date(); // initially contains current date
var e = new Date(); // initially contains current date
                    // note: both dates must initially contain same value
e.setMonth(d.getMonth() + 2);
console.log(d);    
console.log(e);

Replace console.log with alert if necessary. 如有必要,将console.log替换为alert This will work for dates in November and December in case you're wondering. 如果您想知道的话,这将适用于11月和12月的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM