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 ?
Asked
Active
Viewed 607 times
2
-
add your code here – Syed mohamed aladeen May 22 '19 at 12:05
-
Please add your code. – JackOfAshes - Mohit Gawande May 22 '19 at 12:07
-
Possible duplicate of [How to add 30 minutes to a JavaScript Date object?](https://stackoverflow.com/questions/1197928/how-to-add-30-minutes-to-a-javascript-date-object) – VLAZ May 22 '19 at 12:15
2 Answers
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

Vivek Chaudhary
- 163
- 10
-
1It's always a good practice to add some explanation to your answer alongside the code – Towkir May 22 '19 at 14:52