Click here to Skip to main content
13,863,735 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.
Updated 14-Nov-18 8:13am
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).
Rate this: bad
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:[^],[^].

Rate this: bad
Please Sign up or sign in to vote.

Solution 2


Check this[^]...

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
Web04 | 2.8.190214.1 | Last Updated 14 Nov 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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