Luis
Luis

Reputation: 15

How to add minutes to string date

i have a date string from an API that looks like this 10:00 and i need add a 1 and a half, i tried with this but it doesn´t work

    moment('10:00').add(90, 'minute')

i expect this output 11:30

Upvotes: 0

Views: 300

Answers (2)

Scott Marcus
Scott Marcus

Reputation: 65853

See comments inline:

let input = "10:00";
let inputParts = input.split(":");    // Split the string at the colon

let d = new Date();                   // Create new date
d.setUTCHours(+inputParts[0] + 1);    // Get first part of string and add 1
d.setUTCMinutes(30);                  // Adjust by 1.5 minutes

// Output in the format you want
console.log(d.getUTCHours() + ":" + d.getUTCMinutes());

Upvotes: 1

Ketan Ramteke
Ketan Ramteke

Reputation: 10675

let time = "10:00"

const add=(minutes)=>{
  let [hr, min] = time.split(":")
  minutes += parseInt(hr)*60 + parseInt(min)
  hr = Math.floor(minutes/60)
  min = minutes %60
  
  //console.log(hr%12+":"+ min)
  return (hr%12+":"+ min)
}

console.log(add(90))

Upvotes: 1

Related Questions