Add 2 times together JavaScript

前端 未结 4 1654
轮回少年
轮回少年 2020-12-21 12:39

Looking to add time together within javascript.

I have never written any javascript, and as such am struggling with the way to progress.

I have got to add

相关标签:
4条回答
  • 2020-12-21 12:57

    Using moment.js you can convert hh:mm to minutes and add them.

    Example:

    moment.duration("02:45").asMinutes() + moment.duration("02:15").asMinutes()
    

    Result: 300 mins

    So, you can convert minutes to hh:mm:

    function timeConvert1(data) {
      var minutes = data % 60;
      var hours = (data – minutes) / 60;  
      return (hours + “:” + minutes);
    }
    
    0 讨论(0)
  • 2020-12-21 13:03

    For add array of time-format strings (including seconds and without counting days).

    For example:

    Input: times = ['00:00:10', '00:24:00']

    Output 00:24:10

    // Add two times in hh:mm:ss format
    function addTimes(times = []) {
    
        const z = (n) => (n < 10 ? '0' : '') + n;
    
        let hour = 0
        let minute = 0
        let second = 0
        for (const time of times) {
            const splited = time.split(':');
            hour += parseInt(splited[0]);
            minute += parseInt(splited[1])
            second += parseInt(splited[2])
        }
        const seconds = second % 60
        const minutes = parseInt(minute % 60) + parseInt(second / 60)
        const hours = hour + parseInt(minute / 60)
    
        return z(hours) + ':' + z(minutes) + ':' + z(seconds)
    }
    
    0 讨论(0)
  • 2020-12-21 13:08

    Take a look at moment.js - an excellent library for managing all kinds of time related functionality - momentjs.com

    Later addition to answer:

    You mention your are a newbie with JavaScript so here is a simple working example of your problem using moment.js - this example assumes the file and moment.js are in the same folder. Check out the docs on the moment.js for all the formatting options. Good luck.

    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Add Time</title>
    <script src="moment.js"></script>
    </head>
    
    <body>
    
       <script>
    
       //add 4 hours to the stated time
       var theFutureTime = moment().hour('12').minute('44').add(4,'hours').format("HH:mm");
    
       console.log(theFutureTime);  // prints 16:44
    
      </script>
    
    </body>
    

    0 讨论(0)
  • 2020-12-21 13:09

    If you break it down into a couple of small helper functions, it's not too hard:

    // Convert a time in hh:mm format to minutes
    function timeToMins(time) {
      var b = time.split(':');
      return b[0]*60 + +b[1];
    }
    
    // Convert minutes to a time in format hh:mm
    // Returned value is in range 00  to 24 hrs
    function timeFromMins(mins) {
      function z(n){return (n<10? '0':'') + n;}
      var h = (mins/60 |0) % 24;
      var m = mins % 60;
      return z(h) + ':' + z(m);
    }
    
    // Add two times in hh:mm format
    function addTimes(t0, t1) {
      return timeFromMins(timeToMins(t0) + timeToMins(t1));
    }
    
    console.log(addTimes('12:13', '01:42')); // 13:55
    console.log(addTimes('12:13', '13:42')); // 01:55
    console.log(addTimes('02:43', '03:42')); // 06:25
    
    0 讨论(0)
提交回复
热议问题