0

i get this time from an external JSON :

"time":"19:45"
I need to add 2 hours from this string.
Is it possible in JS? Thanks

PeraDev
  • 27
  • 1
  • 4
  • Is this possible in any language?, time does not exist without being a part of the date, if you mean this is today's time, then it will make some sense. – vaira Sep 11 '21 at 00:05
  • sure it does @vaira, every day has a 7 o'clock and 2 hours later it's 9 – pilchard Sep 11 '21 at 00:06
  • what about `23:01`? it will become `01:01`? – Sphinx Sep 11 '21 at 00:12
  • is it "time":"09:24" or "time:":"9:14"? Do you have a leading zero or not? – Strella Sep 11 '21 at 00:27
  • @pilchard the question is '"convert" string to time and add 2 hours in JS', I did not think he was asking for just sting manipulation – vaira Sep 11 '21 at 00:44
  • That's a fair point @vaira (and there are moments when adding 2 hours isn't as straight forward as I made out, depending on the date) – pilchard Sep 11 '21 at 00:47
  • yes, you should convert data from JSON to Js and add 2 hours after that again convert JS to JSON. you can add hours like this - https://stackoverflow.com/questions/1050720/adding-hours-to-javascript-date-object – Rajeev Singh Sep 17 '21 at 05:05
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 17 '21 at 05:27

3 Answers3

1

Try this

let myTime = '19:45'

function getTime(time, addHour) {
  let [h, m] = time.split(':');
  let date = new Date();
   date.setHours(h, m, 0)
   date.toString();
  let res = `${date.getHours()+addHour}:${date.getMinutes()}`
  return res
}
 
console.log(getTime( myTime, 2 ))
pilchard
  • 12,414
  • 5
  • 11
  • 23
GMKHussain
  • 3,342
  • 1
  • 21
  • 19
0

uses String.split to get hourNum and minuteNum, then construct one Date object and uses setTime to add two hours.

function addHours(text, hours=2) {
  const [hourNum, minNum] = text.split(':')
  const time = new Date(0, 0, 0, hourNum, minNum)
  time.setTime(time.getTime() + (hours * 60 * 60 * 1000))
  return `${time.getHours()}:${time.getMinutes()}`
}

console.log(addHours('19:45', 2))
console.log(addHours('23:45', 2))
Sphinx
  • 10,519
  • 2
  • 27
  • 45
  • This may have trouble with changing offsets, `new Date(0,0,0)` actually creates a Date for 1899-12-31 around which time many places changed their offset so the increment may push the time from one offset to another, causing the resulting time to be unexpected. Consider using UTC instead. :-) Why is year 0 treated as 1900? It's an ECMASCript Date object of course! Years 0 to 99 are treated as 1900 to 1999. – RobG Sep 11 '21 at 04:03
0

A Date object isn't necessary to do time mathematics, it just means taking account of minutes and seconds (60) and maybe days (24).

E.g.

// Add time to a timestamp, both in in HH:mm format
// If respectDay is true, hours are % 24
function addTime(start, increment, respectDay = false) {
  let pad = n => ('0' + n).slice(-2);
  let timeToMins = time => time.split(':').reduce((h, m) => h*60 + m*1);
  let minsToTime = (mins, respectDay = false) => `${pad((mins / 60 | 0) % (respectDay? 24 : Number.POSITIVE_INFINITY))}:${pad(mins%60)}`;
  return minsToTime(timeToMins(start) + timeToMins(increment), respectDay);
}

let time = "19:45";
console.log(addTime(time, '8:23'));       // Total time  : 28:08
console.log(addTime(time, '8:23', true)); // As day time : 04:08
RobG
  • 142,382
  • 31
  • 172
  • 209