Click here to Skip to main content
13,799,764 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I want to add 5 minutes to current time using javascript.
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 21-Mar-12 9:35am
Updated 14-Nov-18 8: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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:

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
  Permalink  
v5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,

Check this[^]...
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.181214.1 | Last Updated 14 Nov 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100