0
votes

I have a question about javascript time ranges

i have two input boxes, the client can give start date and end date

below have 3 seasons with date ranges,i need to count the cost for the user entered date range with the mapping with below seasons with prices

Total is 3 seasons, below is the date ranges:

Low Season

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

Cost: $5 per day


High Season

11th January to 30th April 1st July to 31st August

Cost: $10 per day


Peak Season

-15th December - 10th January Cost: $2 per day

i write a code for get the date inputs

$('#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











});

so i need to calculate the total price according to the user input, Ex user enters 2012-05-05 - 2013-01-05 this need to calculate price for 3 season dates which is covering the date range. please hep me for solve this problem, thank you...

2
what is the problem? sounds like..."please do my code for me"..if you have made attempts show code for those attemptscharlietfl
yes i do some codes, but i can't get the right results,this calculate the only only within matched range, i want this fr check fr many seasons. her above i added the my codeuser785533
That's kind of a mountain of code @Suneth, perhaps you could be more direct about what is failing about this code? One thing I might suggest trying is downloading something like jstestdriver and trying to do some test-driven development for this problem :)Will Buck
So which part exactly do you have problem with? After a brief look at your codes, it looks like you are using the user input year for all your reference points. Wouldn't that be subject to change? Can you also be more specific about the part that's not working?gtr32x
user enters 2012-01-05 to 2012-6-5 this need to calculate 150 days, which has two different seasons, the seasonal daily price is different,s how to extract 1st season date count and second season date count. thank you..user785533

2 Answers

0
votes

What you need here, it sounds like, is a little help breaking down your work ;)

I think how I would attack this problem is design a custom dateRangeDistance function. You want to give a start date and find out how many days before one of your range end dates that day is, and then give an end date and calculate how many days ahead of what date range its in. I would also define your date ranges as maps rather than those nasty assortments of variables.

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}
}

Really you could make those functions the same function and pass a second param for whether you're counting from the front or back. This should put you on the right track though. If you need more help you can ask, but you do also need to understand that this is not a site that writes your algorithms for you ;)

Bottom line: leverage some helper functions and mappings of data structures to help break the work down into more manageable chunks!

0
votes

I'm not going to code for you but here's the logic:

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 will contain the total cost/day for the entire date range.