du6jws6975 2017-10-23 06:26
浏览 85
已采纳

计算后24小时输出正确的格式

I'm currently using this function to calculate 2 fields and the results are good but sometimes missing a zero. sample

10:20 + 10:30 current output 0.10

10:20 + 10:30 I want the output to be 00.10

$(function () {
 function calculate() {
         time1 = $("#start").val().split(':'),
         time2 = $("#end").val().split(':');
         hours1 = parseInt(time1[0], 10), 
         hours2 = parseInt(time2[0], 10),
         mins1 = parseInt(time1[1], 10),
         mins2 = parseInt(time2[1], 10);
         hours = hours2 - hours1,
         mins = 0;
     if(hours < 0) hours = 24 + hours;
     if(mins2 >= mins1) {
         mins = mins2 - mins1;
     } else {
         mins = (mins2 + 60) - mins1;
     }

     // the result
     $("#hours").val(hours + ':' + mins);         
 }

});

also when there is an invalid character I keep getting a nan message is possible to change this to 00 instead?

  • 写回答

3条回答 默认 最新

  • doumi1852 2017-10-23 07:06
    关注

    Instead of dealing with the strings and each value independently, you can use the javascript Date object to calculate the difference...

    function calculate() {
    
        // Get time values and convert them to javascript Date objects.
        var time1 = new Date('01/01/2017 ' + $('#start').val());
        var time2 = new Date('01/01/2017 ' + $('#end').val());
        // Get the time difference in minutes. If is negative, add 24 hours.
        var hourDiff = (time2 - time1) / 60000;
        hourDiff = (hourDiff < 0) ? hourDiff+1440 : hourDiff;
        // Calculate hours and minutes.
        var hours = Math.floor(hourDiff/60);
        var minutes = Math.floor(hourDiff%60);
        // Set the result adding '0' to the left if needed        
        $("#hours").val((hours<10 ? '0'+hours : hours) + ':' + (minutes<10 ? '0'+minutes : minutes));
    }
    

    Or even better, you can make the function independent of the DOM elements, so you can reuse it...

    function calculate(startTime,endTime) {
    
        // Get time values and convert them to javascript Date objects.
        var time1 = new Date('01/01/2017 ' + startTime);
        var time2 = new Date('01/01/2017 ' + endTime);
        // Get the time difference in minutes. If is negative, add 24 hours.
        var hourDiff = (time2 - time1) / 60000;
        hourDiff = (hourDiff < 0) ? hourDiff+1440 : hourDiff;
        // Calculate hours and minutes.
        var hours = Math.floor(hourDiff/60);
        var minutes = Math.floor(hourDiff%60);
        // Return the response, adding '0' to the left of each field if needed.       
        return (hours<10 ? '0'+hours : hours) + ':' + (minutes<10 ? '0'+minutes : minutes);
    }
    
    // Now you can use the function.
    $("#hours").val(calculate($('#start').val(),$('#end').val()));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?