1

I like to substract time in javascript. I tried various methods, but none of them works.

For example:

var endtime = "22:30" // time-range 00 to 24 hours
var time_difference = "2:30" // time to substract from endtime
// wanted result: starttime = "20:00" // time

How can i fix this with javascript?

Guido Lemmens 2
  • 2,317
  • 5
  • 23
  • 29

2 Answers2

0

Using moment.js:

var endtime = moment({hour: 22, minute: 30});
endtime.subtract(2, 'hours');
endtime.subtract(30, 'minutes');
var result = endtime.format('H:mm');
mostruash
  • 4,169
  • 1
  • 23
  • 40
  • Nice one down voter! You are so `1337`. – mostruash Jun 29 '15 at 00:39
  • 1
    Was not a downvoter, but your solution may produce wrong results when it's time saving day. It's generally a terrible (or even stupid?) idea to treat time as a date. – zerkms Jun 29 '15 at 00:45
  • @zerkms You can basically base it on a date + timezone in the past that you are sure has nothing to do with timesaving days/leap years/leap seconds. – mostruash Jun 29 '15 at 01:17
  • You can, until you do that - your solution may produce incorrect results (and your answer is still incorrect due to that). – zerkms Jun 29 '15 at 01:18
0

Try using moment.js moment().subtract() method...

http://momentjs.com

temarsden
  • 332
  • 5
  • 16