gpt4 book ai didi

javascript - jQuery 日期范围之间的天数

转载 作者:行者123 更新时间:2023-12-02 17:40:55 24 4
gpt4 key购买 nike

我有一个带有 2 个输入文本(开始和结束)的表单,两者都带有 jquery datapicker。使用 jQuery,我试图获取预定义日期范围内的天数(每年两个学期)以应用不同的费率

For ex:

start-date end-date rate

Range A: 01/01/2012 - 06/30/2012 --> 5%
Range B: 07/01/2012 - 12/31/2012 --> 10%
Range C: 01/01/2013 - 06/30/2013 --> 15%
Range D: 07/01/2013 - 12/31/2013 --> 20%
and so on...

So if date inserted in "start" is = 08/15/2012
and the date inserted in "end" is = 11/20/2013

The number of days per range are:
Range A: 0
Range B: 138 with a rate 10%
Range C: 181 with a rate 15%
Range D: 143 with a rate 20%

所以答案是,如何做这样的事情:

(Math.min(end,rangeX_end-date) - Math.max(start,rangeX_start-date))*rangeX_rate

最佳答案

这里有一个功能可以帮助您..其用法与sql相同..

function datediff(fromDate,toDate,interval) { 
var second=1000, minute=second*60, hour=minute*60, day=hour*24, week=day*7;
fromDate = new Date(fromDate);
toDate = new Date(toDate);
var timediff = toDate - fromDate;
if (isNaN(timediff)) return NaN;
switch (interval) {
case "years": return toDate.getFullYear() - fromDate.getFullYear();
case "months": return (
( toDate.getFullYear() * 12 + toDate.getMonth() )
-
( fromDate.getFullYear() * 12 + fromDate.getMonth() )
);
case "weeks" : return Math.floor(timediff / week);
case "days" : return Math.floor(timediff / day);
case "hours" : return Math.floor(timediff / hour);
case "minutes": return Math.floor(timediff / minute);
case "seconds": return Math.floor(timediff / second);
default: return undefined;
}
}

在更新的 fiddle 中向下滚动以查看“更整洁”的版本; http://jsfiddle.net/uUqrT/5/

$("#calc").click(function () 
{
addDaysRow($('#from').val(), $('#to').val());
});

function addDaysRow(fromDate, toDate) {
var rangeAstart = new Date('01/01/2012');
var rangeAend = new Date('06/30/2012');
var rangeBstart = new Date('07/01/2012');
var rangeBend = new Date('12/31/2012');
var rangeCstart = new Date('01/01/2013');
var rangeCend = new Date('06/30/2013');
var rangeDstart = new Date('07/01/2013');
var rangeDend = new Date('12/31/2013');

var diff = datediff(fromDate, toDate, "days");

$('#record > tbody:last').append('<tr><td>' + diff + '</td><td>' + diff + '</td><td>' +diff + '</td><td>' + diff + '</td><td>' + diff + '</td><td>' + diff + '</td><td>' + diff + '</td></tr>');
}

这应该让您走上正确的轨道 - 不确定您想用这里的范围做什么。

关于javascript - jQuery 日期范围之间的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22229284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com