Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to add two times in javascript

// 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
Comment

PREVIOUS NEXT
Code Example
Javascript :: Next js Linking example 
Javascript :: js exclude from object 
Javascript :: javascript array iteration methods 
Javascript :: discord delete message 
Javascript :: components in react 
Javascript :: nodejs express flash message 
Javascript :: convert integer month to string month react native 
Javascript :: Children in JSX 
Javascript :: jest cannot find module 
Javascript :: react router switch 
Javascript :: javascript get object in object 
Javascript :: latecy discord 
Javascript :: unzip array javascript 
Javascript :: copia independiente array javascript 
Javascript :: filter js object array based on multiple parameters 
Javascript :: javascript /g 
Javascript :: textcontent javascript 
Javascript :: firebase get last document 
Javascript :: make table responsive react-bootstrap-table2 
Javascript :: how to add an event listener to a function javascript 
Javascript :: 100 day javascript challenge 
Javascript :: streami node js 
Javascript :: string length javascript 
Javascript :: npm md to html 
Javascript :: push json data into a list of objects in flutter 
Javascript :: fix slow loading images in react 
Javascript :: onomonrieah 
Javascript :: jquery xpath 
Javascript :: insertadjacenthtml trong js 
Javascript :: json type error at login 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =