JavaScript 无法计算表达式

发布于 2024-11-14 17:32:43 字数 1201 浏览 3 评论 0原文

我正在尝试计算 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄想挽回 2024-11-21 17:32:43

我终于能够找到答案了。我需要将 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.

还如梦归 2024-11-21 17:32:43

不确定要求什么。

对于 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文