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) 2012년 7월 26일 시즌 2 (S2)에서 2012년 1월 1일에서 시작 및 종료 인도 표준시를 시작 IST (1) 비용 1 € 밤 시즌 1 비용이 € 밤
첫 번째와 마지막 날 시즌 2012년 12월 31일
에서2012년 7월 26일에 끝납니다 (하나 때문에 계산이다 먼저 계산하지 않음). | 9 :
시험 A : 범위 (및 가격)이 올바르게 계산됩니다 : 일 범위 : 바이올린 경고를 참조
요약 (종료 날짜는 시즌 종료 날짜보다 낮은) category : 싱글 룸 | 비용 9 | from : 2012-07-17 | ~ : 2012-07-26
테스트 B : (종료일은 시즌 종료일과 동일합니다.) 요약 : 마지막 날짜는 계산되지 않습니다. 범위 및 가격은 올바르게 계산되지 않습니다.
피들 알리미보기 : 일 수 : 9 | category : 싱글 룸 | 비용 9 | from : 2012-07-17 | ~ : 2012-07-27
시험 C : (종료일이 계절 종료일보다 높음) 요약 : 마지막 날과 동일한 날짜는 계산되지 않습니다. 28 번째 beRange (와 Price)는 올바르게 계산되지 않을 것입니다 :
피들 알리미 참조 : 범위 : 일 | category : 싱글 룸 | 비용 11 | from : 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);
}
}
는 이미 사파리 자바 스크립트 오류 로그를 확인했다,하지만 난 거기에 어떤 문제를 찾을 수 없습니다.
혹시 다시 봐도 될까요? 좋습니다 .-)
업데이트 26.7.2012 Safari 6.0 (오늘 발표 됨)에는 문제가 발생하지 않았습니다.
나는 생각 계절이 끝나거나 문제를 오해하고있는 날이기 때문에 작은 아이디어가 하루가 끝나면 하루를 더 추가하려고합니까? 날짜 계산에 문제가 있으면 Date.js –
을 시도해보십시오! ;-) 나는 그것을 시도해 보았지만 var seasonDays = 1 + 을 통해 1 일 추가되었음을 잊어 버렸습니다. 때때로 그러한 지점에 머물러 있으면 기다려야합니다. 맥주를 마시고 나중에 다시 시도하십시오. P 소파에 관심이 있다면 ;-) – user1532132
기꺼이 도와 드리겠습니다 :-P –