JavaScript 无法计算表达式
我正在尝试计算 Javascript 中的一天差异以应用一些值。第一个表达
如果 subs_start
工作正常,但第二个
subs_end
不起作用,同样如此
subs_mid
代码:
var subs_start = 0;
var subs_mid = 0;
var subs_end = 0;
var dayDiff = (end_year*365 + end_mon * 30 + end_day)
- (start_year*365 + start_mon* 30 + start_day);
var oneDay=1000*60*60*24;
var oneHour = 1000*60*60;
var timeDiff = endDate.getTime() - startDate.getTime();
var hourDiff = timeDiff/oneHour;
var start_rem_hour = 24 - start_hour*1;
$.each(subsistence, function(id,subs){
if(subs.start <= start_rem_hour && start_rem_hour < subs.end ){
subs_start = subs.rate;
}
alert('T' + end_hour);
if(subs.start <= end_hour && end_hour < subs.end ){
subs_end = subs.rate;
alert ('e ' + subs_end);
}
if(dayDiff > 2){
if(subs.start >= 10){
subs_mid = subs.rate * (dayDiff - 2);
alert ('m ' + subs_mid);
}
}
});
var subs_allow = subs_start*1 + subs_mid*1 + subs_end*1 ;
I m trying to calculate a day diff in Javascript to apply some value. The first expression
if subs_start
is working fine, but the second one
subs_end
is not working, same goes with
subs_mid
Code:
var subs_start = 0;
var subs_mid = 0;
var subs_end = 0;
var dayDiff = (end_year*365 + end_mon * 30 + end_day)
- (start_year*365 + start_mon* 30 + start_day);
var oneDay=1000*60*60*24;
var oneHour = 1000*60*60;
var timeDiff = endDate.getTime() - startDate.getTime();
var hourDiff = timeDiff/oneHour;
var start_rem_hour = 24 - start_hour*1;
$.each(subsistence, function(id,subs){
if(subs.start <= start_rem_hour && start_rem_hour < subs.end ){
subs_start = subs.rate;
}
alert('T' + end_hour);
if(subs.start <= end_hour && end_hour < subs.end ){
subs_end = subs.rate;
alert ('e ' + subs_end);
}
if(dayDiff > 2){
if(subs.start >= 10){
subs_mid = subs.rate * (dayDiff - 2);
alert ('m ' + subs_mid);
}
}
});
var subs_allow = subs_start*1 + subs_mid*1 + subs_end*1 ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于能够找到答案了。我需要将 start_rem_hour 和 end_hour 与 1 相乘才能转换为 int。 js 似乎将它们视为字符串,当我乘以 1 时,它进入整数范围。
I m finally able to find the answer. I need to multiple start_rem_hour and end_hour with 1 to convert into int. It seems js is taking them as string and when i multiply with 1 it gets into integer scope.
不确定要求什么。
对于 javascript 中的日期时间计算,我推荐 Datejs 库:
http://www.datejs.com/
Not sure what is being asked for.
For DateTime calculations in javascript I would recommend Datejs library:
http://www.datejs.com/