0

I've this function to add minutes to a date in javascript

  function addMinutes(date, minutes) {
            var DateObject = new Date(date);
            var modifiedDate =  DateObject.getTime() + minutes * 60000;
            return date = modifiedDate;
        }   

The script works perfectly on most of my pages but on the current page i'm working on I've this date: 2014-06-07 01:00:00

This works only in google chrome.. I've that browsers like IE/Safari are not able to work with YYYY/MM/DD format.

I've tried to parse it with Date, but this is kinda new for me and I'm not sure what i'm doing wrong.

5
  • 2
    This may help :), somebody on that thread is suggesting the moment.js library which looks pretty promising
    – JMK
    Commented Jun 6, 2014 at 14:53
  • 1
    new Date(String(date).replace(/\ /g,"T")+"Z"); works in all browsers supporting toISOString().
    – dandavis
    Commented Jun 6, 2014 at 14:53
  • @dandavis it does work but there is going something wrong in timezones I think. I've one default time zone so no need for complicated stuff there. When I add 15 minutes to 01:00 it returns Sat Jun 07 2014 03:00:00 GMT+0200 (CEST). How Can I get 01:15 ? Commented Jun 6, 2014 at 15:00
  • 1
    var modifiedDate = DateObject.getTime() + (15* 60000) + (new Date().getTimezoneOffset()*60*1000)
    – dandavis
    Commented Jun 6, 2014 at 15:11
  • @dandavis awesome! can you post it as answer? because it solved my problem Commented Jun 6, 2014 at 15:15

2 Answers 2

1

here's a fixed copy of the orig with suggestions implemented:

  function addMinutes(date, minutes) {
            var DateObject = new Date(String(date).replace(/\ /g,"T")+"Z"),
            modifiedDate = DateObject.getTime() + 
                 (minutes* 60000) + 
                 (new Date(DateObject).getTimezoneOffset()*60*1000) ;
            return date = modifiedDate;
   }   

new Date(addMinutes("2014-06-07 01:00:00", 15)).toLocaleString();
// shows: "6/7/2014 1:15:00 AM"
2
  • Careful - you're getting the offset for the current date, which can be different from the offset for the date in question, due to daylight saving time. Commented Jun 6, 2014 at 15:34
  • @MattJohnson: good point, code adjusted to handle that by feeding the date to the offset getter...
    – dandavis
    Commented Jun 6, 2014 at 19:59
1

There are two different problems here that you should mentally separate:

  1. Parsing a string in the particular format of YYYY-MM-DD hh:mm:ss to a Date object.
  2. Adding minutes to a Date object.

Parsing the String

You should be aware that the parsing behavior when you pass a string to the Date constructor is implementation specific, and the implementations vary between browser vendors.

  • In general, when dashes (-) are present, the values are treated as UTC, and when slashes (-) are present, the values are treated as local to the time zone where the code is running.
  • However, this only applies when either a time is not present, or when the date and time components are separated with a T instead of with a space. (YYYY-MM-DDThh:mm:ss)
  • When a space is used to separate date and time components, some browsers (like Chrome) will treat it as local time, but other browsers (like IE and Firefox) will consider it an invalid date.
  • Replacing the space with a T will allow the date to be parsed, but if that's all you do, then Chrome will treat it as UTC, while IE and Firefox will treat it as local time.
  • If you also add the trailing Z, (YYYY-MM-DDThh:mm:ssZ) then all browsers will parse it as UTC.
  • If you want a format that all browsers will recognize as local time, there is only one, and it's not ISO standard: YYYY/MM/DD hh:mm:ss. Thus, you might consider:

    var s = "2014-06-07 01:00:00";
    var dt = new Date(s.replace(/-/g,'/'));
    

Adding Minutes

This is much more straightforward:

dt.setMinutes(dt.getMinutes() + 15);

That will simply mutate the Date value to add 15 minutes. Don't worry about overflow - if getMinutes returns 55, setting 70 minutes will properly add 1 hour and 10 minutes.

A Better Solution

Moment.js removes all of the guesswork about parsing variations, and gives you a much cleaner API. Consider:

// parse a string using a specific format
var m = moment("2014-06-07 01:00:00","YYYY-MM-DD HH:mm:ss");

// adding time
m.add(15, 'minutes');

// format the output as desired, with lots of options
var s = m.format("L h:mm:ss A");
1
  • Thanks for the long answer and explenation of the real problem. will step over to use of moment js :) Commented Jun 10, 2014 at 9:14

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