gpt4 book ai didi

javascript - 范围价格计算

转载 作者:行者123 更新时间:2023-11-28 09:47:50 26 4
gpt4 key购买 nike

我正在使用此代码片段的自定义版本来实现基于 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');

编辑:首先感谢您的快速回复:

我制作了一个新的 fiddle ,我可以在其中显示问题。抱歉我回复晚了,但我正在做饭;-)

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

第一季每晚收费 1 欧元第 1 季每晚 2 欧元

第一天和最后一天算作一天(因此第一天不算)。

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

查看 fiddle 警报:天数范围: 9 |类别: 单人间 |成本 9 |来源:2012-07-17 |至:2012-07-26

测试 B:(结束日期与赛季结束日期相同)摘要:最后一个日期不算数。范围(和价格)将无法正确计算:

查看 fiddle 警报:天数范围: 9 |类别: 单人间 |成本 9 |来源:2012-07-17 |至:2012-07-27

测试 C:(结束日期晚于赛季结束日期)摘要:等于最后一天的那一天不计算在内。第 28 个将是范围(和价格)将无法正确计算:

查看 fiddle 警报:天数范围: 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 错误日志,但没有发现任何问题。

各位可以再看一下吗?那就太好了;-)

更新 2012 年 7 月 26 日在 Safari 6.0(今天发布)上没有出现该问题。

最佳答案

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

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

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

-编辑-:
逻辑看起来不对。
如果您提交:calculatePrice('single-room', '2012-05-31', '2012-05-31');价格为 0。
这是因为开始时间不少于赛季结束时间。 == 赛季结束了。但仍然是有效时间。
if ( 开始 < s.end && end > s.start ) {
因此应该是:
if ( 开始 <= s.end && 结束 > s.start ) {

关于javascript - 范围价格计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11525412/

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