Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I want to add 5 minutes to current time using javascript.
C#
var minutesforExpiry = parseInt("30") - parseInt("25");
var timetoExpire = new Date();
var minutestoExpire = parseInt(timetoExpire.getMinutes()) + minutesforExpiry;

But If minutes is 56 at that time I am getting 61 which is not correct.
Posted
Updated 14-Nov-18 7:13am
Comments
Sergey Alexandrovich Kryukov 21-Mar-12 14:55pm    
I voted 4 for correctly posed question (this happens so rarely, unfortunately!). Not 5 because you could easily find out the way using those short documentation pages (referenced in my answer, please see).
--SA

You pointed out this problem correctly. I was amazed how many incorrect "solutions" people offer, ignoring this "overflow" problem you noticed!

One of the right ways is converting time into milliseconds and adding required duration in milliseconds:

JavaScript
momentOfTime = new Date(); // just for example, can be any other time
myTimeSpan = 5*60*1000; // 5 minutes in milliseconds
momentOfTime.setTime(momentOfTime.getTime() + myTimeSpan);


Please see:
http://www.w3schools.com/jsref/jsref_obj_date.asp[^],
http://www.w3schools.com/jsref/jsref_gettime.asp[^].

—SA
 
Share this answer
 
v5
Hi,

Check this[^]...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900