有人能指出我正确的方向吗?我试图找出特定范围内有多少天。如果我有预订日期范围,例如'2013年1月18日21:00'和'2013年2月4日15:00',并想知道2月1日到4月30日之间有多少天。经过大量的摆弄,我可能会有它......?
/* The test date range */
var startTime = "18 Jan 2013 21:00";
var endTime = "4 Feb 2013 15:00";
/* Set up the range to test against*/
var rangeStart = Date.parse ("1 Feb 2013 00:00");
var rangeEnd = Date.parse ("30 Apr 2013 00:00");
var start = Date.parse(startTime);
var end = Date.parse(endTime);
var numOfHrs = Math.ceil((end-start) / (1000*60*60));
var numOfDays = Math.ceil(numOfHrs/24);
var rangeCrossed = 0;
if (end > rangeStart){ /* A valid crossover period may exist */
/* Start time before or equal range start and end less than or equal the range end */
if ((start<=rangeStart) && (end <= rangeEnd)) { rangeCrossed = end-rangeStart }
/*Start greater than or equal to the range start and less than the range end */
else if ((start>=rangeStart) && (start<rangeEnd))
{
/* start and end both within range */
if (end<=rangeEnd) { rangeCrossed = end-start }
/* start after range start and end after range end */
else {rangeCrossed = (rangeEnd-start) + (end-rangeEnd)};
}
/*full range crossed */
else if (start<=rangeStart && end >=rangeEnd) { rangeCrossed = rangeEnd-rangeStart};
}
alert((rangeCrossed/(1000*60*60)/24))