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.

JochemQuery
  • 1,495
  • 2
  • 26
  • 38
  • 2
    [This](http://stackoverflow.com/q/1056728/969613) may help :), somebody on that thread is suggesting the moment.js library which looks pretty promising – JMK Jun 06 '14 at 14:53
  • 1
    new Date(String(date).replace(/\ /g,"T")+"Z"); works in all browsers supporting toISOString(). – dandavis Jun 06 '14 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 ? – JochemQuery Jun 06 '14 at 15:00
  • 1
    var modifiedDate = DateObject.getTime() + (15* 60000) + (new Date().getTimezoneOffset()*60*1000) – dandavis Jun 06 '14 at 15:11
  • @dandavis awesome! can you post it as answer? because it solved my problem – JochemQuery Jun 06 '14 at 15:15

2 Answers2

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"
dandavis
  • 16,370
  • 5
  • 40
  • 36
  • 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. – Matt Johnson-Pint Jun 06 '14 at 15:34
  • @MattJohnson: good point, code adjusted to handle that by feeding the date to the offset getter... – dandavis Jun 06 '14 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");
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575