javascript计算日期范围的价格

时间:2012-03-06 19:42:51

标签: javascript jquery date range

我对javascript时间范围有疑问

我有两个输入框,客户端可以给出开始日期和结束日期

下面有3个季节有日期范围,我需要计算用户输入日期范围的成本 下面的季节与价格的映射

总计是3个季节,以下是日期范围:

淡季

5月1日至6月30日 9月1日至12月14日

费用:每天5美元


旺季

1月11日至4月30日 7月1日至8月31日

费用:每天10美元


旺季

12月1日至1月10日 费用:每天2美元

我写了一个代码来获取日期输入

$('#calculate').click(function(){
//get the first date
da=$('#daydropdowna').val();
ma=$('#monthdropdowna').val();
ya=$('#yeardropdowna').val();



//get the secnd date
dd=$('#daydropdownd').val();
md=$('#monthdropdownd').val();
yd=$('#yeardropdownd').val();



var oneDay = 24*60*60*1000; 
var ourstart= new Date(ya,ma,da);
var oursend= new Date(yd,md,dd);


/*
11th January to 30th April 
1st July to 31st August 
*/
var hsstart= new Date(ya,00,11);
var hsend= new Date(ya,03,11);
var hsstart1= new Date(ya,06,01);
var hsend1= new Date(ya,07,31);


/*
1st May to 30th June 
1st September to 14th December 
*/

var lsstart=new Date(ya,04,01);
var lsend=new Date(ya,05,30);
var lsstart1=Date(ya,08,01);
var lsend1=new Date(ya,11,14);


/*
-15th December - 10th January 
*/
var psstart=new Date(ya,11,15);
var psend=new Date(ya+1,00,10);


var myDate = ourstart;
var myDate1 = oursend;




var startDate = hsstart
var endDate = hsend



//the date range within one high season

if ((startDate < myDate) && (myDate < endDate) && (startDate < myDate1) && (myDate1 < endDate)) {
        alert('seasn 1 h');
       var diffDays = Math.round(Math.abs((myDate1 .getTime() - myDate .getTime())/(oneDay)));

alert (diffDays);
}


//the date range within secnd high season
var startDate = hsstart1
var endDate = hsend1

if ((startDate < myDate) && (myDate < endDate) && (startDate < myDate1) && (myDate1 < endDate)) {
        alert('seasn 2 h');
       var diffDays = Math.round(Math.abs((myDate1 .getTime() - myDate .getTime())/(oneDay)));

alert (diffDays);
}


//the date range within first low season
var startDate = lsstart
var endDate = lsend

if ((startDate < myDate) && (myDate < endDate) && (startDate < myDate1) && (myDate1 < endDate)) {
        alert('season 1 l');
       var diffDays = Math.round(Math.abs((myDate1 .getTime() - myDate .getTime())/(oneDay)));

alert (diffDays);
}



//the date range within second low season
var startDate = lsstart1
var endDate = lsend1



if ((startDate < myDate) && (myDate < endDate) && (startDate < myDate1) && (myDate1 < endDate)) {
        alert('season 2 l');
       var diffDays = Math.round(Math.abs((myDate1 .getTime() - myDate .getTime())/(oneDay)));

alert (diffDays);
}

//peak
var startDate = psstart
var endDate = psend

if ((startDate < myDate) && (myDate < endDate) && (startDate < myDate1) && (myDate1 < endDate)) {
        alert('season p');
       var diffDays = Math.round(Math.abs((myDate1 .getTime() - myDate .getTime())/(oneDay)));

alert (diffDays);
}


//if not wihin a specific range cacuate for all











});

所以我需要根据用户输入计算总价,Ex用户输入2012-05-05 - 2013-01-05这个需要计算3个季节日期的价格,这个日期范围涵盖了。请帮我解决这个问题,谢谢......

2 个答案:

答案 0 :(得分:0)

这里你需要的,听起来像是有点帮助打破你的工作;)

我认为如何解决这个问题是设计一个自定义的dateRangeDistance函数。您想要提供一个开始日期,并找出当天的某个范围结束日期之前的天数,然后给出结束日期并计算其日期范围之前的天数。我还会定义您的日期范围作为地图而不是那些令人讨厌的变量分类。

var peakSeason = {startDate: w/e, endDate: w/e, numDays: w/e, costRatePerDay: w/e}
var lowSeason1 = {startDate: w/e, endDate: w/e, numDays: w/e, costRatePerDay: w/e}
var lowSeason2 = {startDate: w/e, endDate: w/e, numDays: w/e, costRatePerDay: w/e}
// .... etc
var allSeasons = [peakSeason, lowSeason1, ...] // put these in order and it can help iterating date ranges between

var numberDaysStartFromEndOfRange = function(startDate) {
    // iterate through the allSeasons map and find what range this is in 
    // return {numDays: whateverYouCalculate, dateRange: name your date ranges uniquely}
}

var numberDaysEndFromStartOfRange = function(startDate) {
    // iterate through the allSeasons map and find what range this is in
    // return {numDays: whateverYouCalculate, dateRange: name your date ranges uniquely}
}

实际上你可以使这些功能具有相同的功能,并传递第二个参数,无论你是从前面还是后面计数。这应该会让你走上正轨。如果您需要更多帮助,可以询问,但您还需要了解这不是为您编写算法的站点;)

结论:利用一些辅助函数和数据结构的映射来帮助将工作分解为更易于管理的块!

答案 1 :(得分:0)

我不会为你编码,但这是逻辑:

tempDate = inputStartDate
for each season
    if seasonStart <= tempDate
        if inputEndDate <= seasonEnd
            price = price + (inputEndDate - tempDate) * seasonCost
            break
        else
            price = price + (seasonEnd - tempDate) * seasonCost
            tempDate = seasonEnd + 1 day

price将包含整个日期范围的总费用/天。