20

I have a date string that looks like the following javascript format. I want to convert this to a date object and add one minute.

timeObject = "Mon Nov 07 2011 06:41:48 GMT-0500 (Eastern Standard Time)";


timeObject.setSeconds(timeObject.getSeconds() + 60);

====== SOLUTION ==========

never mind. I got it...

var time = $('#myDiv').val();     // = "Mon Nov 07 2011 06:41:48 GMT-0500 (Eastern Standard Time)";
var timeObject = new Date(time);                
alert(timeObject);
timeObject.setSeconds(timeObject.getSeconds() + 60);    
alert(timeObject);
2
  • You should post your solution as an answer and accept it.
    – zatatatata
    Commented Nov 7, 2011 at 12:10
  • 1
    system would not let me. not enough points. Commented Nov 7, 2011 at 12:13

2 Answers 2

36

Proper way is:

timeObject.setTime(timeObject.getTime() + 1000 * 60);
1
  • If the provided value doesn't contain a correct datetime value, but only times (ex. 06:42), use var timeObject = new Date("2000-01-01 " + time); to get a valid datetime object.
    – Stronghold
    Commented Oct 10, 2021 at 0:36
0

Another approach could be using .setMinutes():

timeObject.setMinutes(timeObject.getMinutes() + 1)

Even if adding n minute(s) would cause a change of hour/day/month/year, the Date object already calculates this for you so you don't have to worry. Check here for an example.

2
  • 1
    You're wrong @ganzux. setMinutes() (and any other setMETHOD() for that matter) already calculates this for you. Check this example out for more information. Please check if your conclusions are correct before telling everyone about them.
    – Zebiano
    Commented Mar 1 at 14:25
  • you are absolutely right, I am editing that to not get anyone confused.
    – ganzux
    Commented Mar 1 at 19:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.