范围价格计算

时间:2012-07-17 15:04:18

标签: javascript jquery html

我正在使用此代码段的自定义版本来构建基于jQuery的租借预订脚本。现在我在这个脚本中发现了一个问题。我花了好几个小时来解决问题,但我无法修复它。

如果最后一个租赁日也是本赛季的最后一天,则会出现问题。如果发生这种情况,则不计算和计算当天。首先我没有认出它,因为如果最后一天晚于赛季的最后一天,那么剧本就可以了。

如果有人能帮助我弄清楚我的问题,那就太好了。

jsFiddle demo

这是代码。

var MILLI_PER_DAY = 86400000;
/**
 * identifier is a string that identify the season. like 'summer'
 * start and end must be string with date pattern: yyyy/MM/dd
 */
var Season = function(identifier, start, end) {
        this.id = identifier
        this.start = new Date(start);
        this.end = new Date(end);
    }
    /**
     * name is the product name
     * prices is an object that defines the price of each season.
     * e.g. {'summer' : 29.9, 'winter' : 35}
     */
var Product = function(name, prices) {
        this.name = name;
        this.prices = prices;
    }
var seasons = [
new Season('s1', '2012-01-01', '2012-02-28'),
new Season('s2', '2012-03-01', '2012-05-31')];
var products = [
new Product('single-room', {
    's1': 16,
    's2': 12
})];
/**
 * productName is the product name to be bought
 * dateStart and dateEnd is the range that productName will be used and
 * they should be a string representing a date with pattern: yyyy/MM/dd
 */
function calculatePrice(productName, dateStart, dateEnd) {
    var start = new Date(dateStart);
    var end = new Date(dateEnd);
    //finding product
    var product = null;
    for (var i = 0; i < products.length; i++) {
        var p = products[i]
        if (p.name == productName) {
            product = p;
            break;
        }
    }
    if (product != null) {
        var totalPrice = 0;
        for (var i = 0; i < seasons.length; i++) {
            var s = seasons[i]
            //if this range contains parts or all the season range
            if (start < s.end && end > s.start) {
                var seasonRange = Math.min(s.end, end) - Math.max(s.start, start);
                //due to the start day must count
                var seasonDays = 1 + (seasonRange / MILLI_PER_DAY);
                totalPrice += product.prices[s.id] * seasonDays;
            }
        }
        alert(product.name + " cost " + totalPrice + " in dates from " + dateStart + " to " + dateEnd);
    }
}
calculatePrice('single-room', '2012-02-08', '2012-03-10');
calculatePrice('single-room', '2012-03-05', '2012-05-10');
calculatePrice('single-room', '2012-01-05', '2012-02-10');

修改 首先,感谢您的快速回复:

我已经制作了一个新的小提琴,我可以在这里展示这个问题。对不起,我迟到的回复,但我正在做饭; - )

首先是事实: 第1季(第1季)开始于2012/01/01,结束于2012/07/26 第2季(第2季)开始于2012/07/26,并于2012/12/31结束

第1季每晚花费1欧元 第1季每晚2欧元

第一天和最后一天计为一个(所以第一天不算)。

测试A :(结束日期低于季节结束日期) 摘要:范围(和价格)将正确计算:

查看小提琴警报: 天数范围:9 |类别:单人房|费用9 |来自:2012-07-17 |至:2012-07-26

测试B :(结束日期与季节结束日期相同) 摘要:最后一个日期不会计算。范围(和价格)不能正确计算:

查看小提琴警报: 天数范围:9 |类别:单人房|费用9 |来自:2012-07-17 |至:2012-07-27

测试C :(结束日期高于季节结束日期) 摘要:等于最后一天的那天不计算在内。第28届将是兰格(和价格)不能正确计算:

查看小提琴警报: 天数范围:10 |类别:单人房|费用11 |来自:2012-07-17 |至:2012-07-28

The Fiddle


该死,现在我有一个真正的问题。它不适用于Sarafi(尝试过Mac版本),甚至iPhone也不能控制价格。 Firefox和Chrome在OSX Lion下运行良好。

价格未被计算似乎脚本停在这些线上:

 if ( product != null ) {
    var totalPrice = 0;
    for ( var i=0; i < seasons.length; i++ ) {
      var s = seasons[i]
      //if this range contains parts or all the season range
      if ( start < s.end && end > s.start ) {
        var seasonRange = Math.min(s.end,end) - Math.max(s.start,start);
        //due to the start day must count
        var seasonDays = 1 + (seasonRange/MILLI_PER_DAY);
        totalPrice += product.prices[s.id]*seasonDays;
      }
    }
    alert(product.name + " cost " + totalPrice + " in dates from " + dateStart + " to " + dateEnd);
  }
}

我已经检查了safari javascript错误日志,但我发现没有问题。

你可以再看一遍吗?这会很棒; - )

更新26.7.2012 Safari 6.0(今天发布)上没有出现此问题。

1 个答案:

答案 0 :(得分:0)

我认为你的字符串日期会被错误地转换为日期 你所有的约会都过早了一天 如果您在控制台中运行:新日期('2012-05-25');你会看到你得到的价值 我改为:新日期('2012/05/25');它似乎工作正常。

calculatePrice( '单室', '2012/05/25', '2012/05/30');

编辑:这是我的控制台输出:
新日期('2012-03-10')
2012年5月9日星期五18:00:00 GMT-0600(中央标准时间)
新日期('2012/03/10')
2012年3月10日星期六00:00:00 GMT-0600(中央标准时间)

CNC中:
逻辑错误。
如果你提交:calculatePrice('single-room','2012-05-31','2012-05-31');你得到0的价格。
这是因为开始不低于赛季结束。它是==到赛季结束。但仍然是一个有效的时间 if(start&lt; s.end&amp;&amp; end&gt; s.start){
因此应该是:
if(start&lt; = s.end&amp;&amp; end&gt; s.start){

相关问题