- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用此代码片段的自定义版本来实现基于 jQuery 的租赁预订脚本。现在我发现这个脚本有问题。我花了几个小时来解决这个问题,但我无法解决它。
如果最后租赁日也是季节的最后一天,就会出现问题。如果发生这种情况,这一天就不会被计算和计算。首先,我还没有识别出它,因为如果最后一天晚于季节的最后一天,则脚本可以正常工作。
如果有人能帮助我解决我的问题,那就太好了。
这是代码。
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
<小时/>该死,现在我遇到了一个真正的问题。它在 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/
SQL 和一般开发的新手,我有一个表(COUNTRIES),其中包含字段(INDEX、NAME、POPULATION、AREA) 通常我添加一个客户端(Delphi)计算字段(DENSITY)和 On
我想使用 calc(100%-100px),但在我的 demo 中不起作用由于高度只接受像素,因此如何将此百分比值转换为像素。 最佳答案 以下将为您提供高度: $(window).height();
我正在尝试在 MySQL 中添加列并动态填充其他列。 例如我有一张表“数字”并具有第 1 列、第 2 列、第 3 列,这些总数应填充在第 4 列中 最佳答案 除非我误解了你的问题,否则你不只是在寻找:
我想返回简单计算的结果,但我不确定如何执行此操作。我的表格如下: SELECT COUNT(fb.engineer_id) AS `total_feedback`, SUM(fb.ra
我一直在尝试做这个程序,但我被卡住了,我仍然是一个初学者,任何帮助将不胜感激。我需要程序来做 打印一个 10 X 10 的表格,其中表格中的每个条目都是行号和列号的总和 包含一个累加器,用于计算所有表
这个计算背后一定有一些逻辑。但我无法得到它。普通数学不会导致这种行为。谁能帮我解释一下原因 printf ("float %f\n", 2/7 * 100.0); 结果打印 1.000000 为什么会
我想计算从 0 到 (n)^{1/2} - 1 的数字的 AND每个数字从 0 到 (n)^{1/2} - 1 .我想在 O(n) 中执行此操作时间,不能使用 XOR、OR、AND 运算。 具体来说,
如何在 Excel 中将公式放入自定义数字格式?例如(出于说明目的随机示例), 假设我有以下数据: 输入 输出 在不编辑单元格中的实际数据的情况下,我想显示单元格中的值除以 2,并保留两位小数: 有没
每次我在 Flutter 应用程序中调用计算()时,我都会看到内存泄漏,据我所知,这基本上只是一种生成隔离的便捷方法。我的应用程序内存占用增加并且在 GC 之后永远不会减少。 我已将我的代码简化为仅调
我有数字特征观察 V1通过 V12用于目标变量 Wavelength .我想计算 Vx 之间的 RMSE列。数据格式如下。 每个变量“Vx”以 5 分钟的间隔进行测量。我想计算所有 Vx 变量的观测值
我正在寻找一种使用 C 语言计算文件中未知字符数的简单方法。谢谢你的帮助 最佳答案 POSIX 方式(可能是您想要的方式): off_t get_file_length( FILE *file ) {
我正在使用 Postgres,并且我正试图围绕如何在连续日期跨度中得出第一个开始日期的问题进行思考。例如 :- ID | Start Date | End Date =================
我有一个订单表格,我在其中使用 jQuery 计算插件来汇总总数。 此求和工作正常,但生成的“总和”存在问题。总之,我希望用逗号替换任何点。 代码的基础是; function ($this) {
我在使用 double 变量计算简单算术方程时遇到问题。 我有一个具有 double 属性 Value 的组件,我将此属性设置为 100。 然后我做一个简单的减法来检查这个值是否真的是 100: va
我在这里看到了一些关于 CRC 32 计算的其他问题。但没有一个让我满意,因此是这样。 openssl 库是否有任何用于计算 CRC32 的 api 支持?我已经在为 SHA1 使用 openssl,
当我在PHP日期计算中遇到问题时,我感到惊讶。 $add = '- 30 days'; echo date('Y-m-01', strtotime($add)); // result is 2017-
我正在使用 javascript 进行练习,我编写了这个脚本来计算 2 个变量的总和,然后在第三个方程中使用这个总和!关于如何完成这项工作的任何想法都将非常有用! First Number:
我有一个来自EAC的提示单和一个包含完整专辑的FLAC文件。 我正在尝试制作一些python脚本来播放文件,因为我需要能够设置在flac文件中开始的位置。 如何从CueSheet格式MM:SS:FF转
这个问题已经有答案了: Adding two numbers concatenates them instead of calculating the sum (24 个回答) 已关闭去年。 我有一个
4000 我需要上面字段 name="quantity" 和 id="price" 中的值,并使用 javascript 函数进行计算,并将其显示在字段 id= 中仅当我单击计算按钮时才显示“总
我是一名优秀的程序员,十分优秀!