我使用此代碼段用於基於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');
編輯: 首先,在所有感謝您快速作出迴應:
我已經做出了新的小提琴,其中蔭能夠顯示的問題。對不起,我遲到了,但我正在做飯;-)
首先的事實: 第1季(s1)ist從2012/01/01開始到2012/07/26結束 第2季(s2)ist開始在2012年7月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日將是Range(和價格)不會被正確計算:
看到小提示: 範圍在天:10 |類別:單房|成本11 |來自:2012-07-17 |於:2012-07-28
該死的,現在我有一個真正的問題。它不會在Sarafi上工作(嘗試Mac版本),甚至iPhone也無法計算價格。 Firefox和Chrome在OSX Lion下運行良好。
的價格wouln't被計算似乎腳本圍繞這些線停止:
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錯誤日誌,但我沒有發現任何的問題在那裏。
請問你可以再看一遍嗎?這將是偉大的;-)
UPDATE 26.7.2012 這個問題沒有發生在Safari 6.0(今天發佈)。
我認爲有一個小的想法試圖增加一天到一個賽季結束,因爲那一天是賽季結束還是我誤解了這個問題?如果你有日期計算的問題,你可以試試Date.js – 2012-07-17 15:25:04
你是對的! ;-)我甚至嘗試過,但我忘記了有一天通過var seasonDays = 1 + 加入添加有時候,如果你堅持這樣一個觀點,你應該等待......喝一杯啤酒,然後再試一次:P 如果您對沙發感興趣,請告訴我;-) – user1532132 2012-07-17 19:09:35
會很高興提供幫助:-P – 2012-07-17 19:41:41