2

I have a time say for eg : 10:59:02 Now i need to add 2 minutes here & it should give me time as 11:01:02. I tried but was not able to achieve Can anyone help me with this ?

Jignesh Mistry
  • 2,221
  • 6
  • 25
  • 50

2 Answers2

3

Try using moment library.

var date = moment('10:59:02 PM', 'hh:mm:ss A')
    .add(2, 'minutes')
    .format("hh:mm:ss");
console.log(date);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.7.0/moment.min.js" type="text/javascript"></script>
hashed_name
  • 553
  • 6
  • 21
2

Actually, you need to convert the time/date in Unix timestamp then you can add the minutes and then again pass that timestamp in Date constructor. you get the time as you needed.

var unixTimestamp = Math.round(new Date("2019-05-22 15:10:00").getTime()/1000); // convert to unixtimestamp
console.log(unixTimestamp);

unixTimestamp += 120 // added 2 min
date = new Date(unixTimestamp * 1000); // again convert to date
console.log(date); // got your solution.
hashed_name
  • 553
  • 6
  • 21