如何根据Javascript中的日期计算预订价格 [英] How to calculate booking price depending on dates in an array in Javascript

查看:150
本文介绍了如何根据Javascript中的日期计算预订价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日期变量:aankomst(到达)和vertrek(离开)。我的目标是计算从入住到出发的总价,这取决于淡季,旺季和旺季。我认为我有一个很好的理论来说明如何实现这个目标,到目前为止,我所做的大部分脚本都在工作。

最后的for循环是我失败的地方。在那一步,我想从lowSeason,midSeason或peakSeason中找到阵列中的当前日期,并将与该季节相关的costprice添加到总价中。这里是代码,我添加了评论,以显示我试图做的每一步,我也添加了大量的警报,所以我确定一切工作,直到for循环:

  //在加载时触发函数并获取到达日期和出发日期并创建数组
window.onload = function getPrices(){
var aankomst = new日期();
var vertrek = new Date(aankomst.getTime()+(48 * 60 * 60 * 1000));
var maand = aankomst.getMonth();
var verblijfDagen = [];
var day = 1000 * 60 * 60 * 24;
var diff =(vertrek.getTime() - aankomst.getTime())/ day;

//计算从到达日期到出发日期的所有单独日期,并将它们放入数组
for(var i = 0; i< = diff; i ++)
{
var xx = aankomst.getTime()+ day * i;
var yy = new Date(xx);
var zz =(yy.getDate()+ - +(yy.getMonth()+ 1)+ - + yy.getFullYear());
var parts = zz.split(' - ');
var zzDate = new Date(parts [2],parts [1] -1,parts [0]);
//检查这个日期是否在正确的日期结构中
alert(zzDate instanceof Date);
verblijfDagen.push(zzDate)
}
//检查所有天数是否在数组
alert(verblijfDagen);

//声明本季节不同季节和每日成本的周期
var peakSeason = {startDate:new Date(2017,10-1,01),endDate:new日期(2017,12-1,31),costRate:500};
var midSeason = {startDate:new Date(2017,5-1,1),endDate:new Date(2017,9-1,30),costRate:400};
var lowSeason = {startDate:new Date(2017,1-1,1),endDate:new Date(2017,4-1,30),costRate:300};
var allSeasons = [peakSeason,midSeason,lowSeason]
//检查这个日期是否在正确的Date结构中
alert(lowSeason.startDate instanceof Date);
//检查日期是否正确
alert(lowSeason.startDate);

// PROBLEM AREA检查数组中有多少个日期并开始循环
var arrayLength = verblijfDagen.length;
for(var u = 0; u< arrayLength; u ++){
//检查数组的所有日期是否循环
alert(verblijfDagen [u]);
//迭代allSeasons来查找日期所属的位置
if(verblijfDagen [u] <= allSeasons.StartDate&& allSeasons.verblijfDagen [u]> = endDate){
//将此日期的costRate添加到totalPrice
var totalPrice = totalPrice + costRate;
}
}
//返回总价
alert(totalPrice);





$ b - 我用循环做了什么错误,我如何解决它?
- 在对象lowSeason,midSeason和peakSeason,我宣布startDate和endDate我宣布一年,所以它目前只适用于2017年,但每个赛季的开始日期和结束日期应该工作的所有年份,怎么可以我编辑这个?如果需要的话,我会问一个单独的问题。



谢谢大家!

解决方案<



$ b

我在循环中犯了什么错误,我该如何解决? >您应该在循环之前声明 totalPrice 变量并将其设置为零,以便您可以在循环中累积您的价格。此外, allSeasons 变量是一个对象数组,但您将其视为一个简单的对象。所以你可以解决这个问题的一个方法就是在第一个循环中循环第二个循环来遍历 allSeasons 数组和比较日期。最后,在条件语句中混合了< = > = 。这是一个可能的解决方案:

var totalPrice = 0; (var j = 0; j if(verblijfDagen.length; u ++){
u]> = allSeasons [j] .startDate&& verblijfDagen [u]< = allSeasons [j] .endDate){
//将此日期的costRate加到totalPrice
totalPrice = totalPrice + allSeasons [j] .costRate;
}
}
}


I have two date vars: aankomst (arrival) and vertrek (departure). My goal is to calculate the total price of the stay from arrival to departure, which depends on low season, mid season and high season. I think I have a good theory of how to achieve this, and most of the script I made so far is working.

The final for loop is where I fail though. At that step I want to find the current date from the array in lowSeason, midSeason or peakSeason and add the costprice associated with that season to the total price. Here is the code, i added comments to show what i tried to do with each step and i also added plenty of alerts so i'm sure everything works until the for loop:

//fire function on load and  get the arrival date and departure date and make the array
window.onload = function getPrices() {
var aankomst = new Date();
var vertrek = new Date(aankomst.getTime() + (48 * 60 * 60 * 1000));
var maand = aankomst.getMonth();
var verblijfDagen = [];
var day = 1000*60*60*24;
var diff = (vertrek.getTime()- aankomst.getTime())/day;

//calculate all seperate days from arrival date to departure date and put them in an array   
for(var i=0;i<=diff; i++)
{
   var xx = aankomst.getTime()+day*i;
   var yy = new Date(xx);
   var zz = (yy.getDate()+"-"+(yy.getMonth()+1)+"-"+yy.getFullYear());
   var parts = zz.split('-');
   var zzDate = new Date(parts[2],parts[1]-1,parts[0]);
   //check if this date is in the correct Date structure
   alert(zzDate instanceof Date);
   verblijfDagen.push(zzDate)
}
//check if all the days are in the array
alert (verblijfDagen);

//declare the period of the different seasons and the daily cost rate during this season 
var peakSeason = {startDate: new Date(2017,10-1,01), endDate: new Date(2017,12-1,31), costRate: 500};
var midSeason = {startDate: new Date(2017,5-1,1), endDate: new Date(2017,9-1,30), costRate: 400};
var lowSeason = {startDate: new Date(2017,1-1,1), endDate: new Date(2017,4-1,30), costRate: 300};
var allSeasons = [peakSeason, midSeason, lowSeason]
//check if this date is in the correct Date structure
alert(lowSeason.startDate instanceof Date);
//check if the date is correct
alert (lowSeason.startDate);

//PROBLEM AREA check how many dates there are in the array and start the loop
var arrayLength = verblijfDagen.length;
for (var u = 0; u < arrayLength; u++) {
    //check if all dates of the array are looped
    alert(verblijfDagen[u]);
    //Iterate through allSeasons to find where the date belongs
    if (verblijfDagen[u] <= allSeasons.StartDate && allSeasons.verblijfDagen[u] >= endDate) {
    //Add costRate of this date to totalPrice
     var totalPrice = totalPrice + costRate;
    }
}
//return the total price
alert(totalPrice);
} 

Question: - What mistakes did i make with the loop and how can i fix it? - In the objects lowSeason, midSeason and peakSeason, i declared startDate and endDate i declared a year, so it currently only works for 2017, but the starting dates and end dates for each season should work for all years, how can i edit this? If needed, i'll ask this in a seperate question.

Thanks everyone!

解决方案

What mistakes did I make in the loop and how can I fix it?

You should declare the totalPrice variable before the loop and set it equal to zero so you can accumulate your price within the loop. Also the allSeasons variable is an array of objects but you were treating it as a simple object. So a way you can solve this is to do a second for loop within the first one to loop through the allSeasons array and compare dates. Lastly, the <= and >= were mixed up in the conditional statement. Here is a possible solution:

var totalPrice = 0; for (var u = 0; u < verblijfDagen.length; u++) { for(var j = 0; j < allSeasons.length; j++) { if (verblijfDagen[u] >= allSeasons[j].startDate && verblijfDagen[u] <= allSeasons[j].endDate) { //Add costRate of this date to totalPrice totalPrice = totalPrice + allSeasons[j].costRate; } } }

这篇关于如何根据Javascript中的日期计算预订价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆