Skip to main content
38 events
when toggle format what by license comment
Mar 23, 2023 at 17:26 comment added Oleg Abrazhaev date-fns.org is the best Date library in JS
Mar 6, 2023 at 1:28 history edited Kip CC BY-SA 4.0
update linked libraries, and use https links
Apr 2, 2021 at 13:30 comment added Duncan Lukkenaer Great advice to use a library. However, as of September 2020 Moment.js is considered legacy and should no longer be used in most cases. See my answer for details.
Jan 27, 2020 at 22:36 comment added Dave Folks, take @Kip 's advice and just use a library! +1 for moment.js! It took less time to set up and use than figuring this out on my own.
Jul 31, 2019 at 18:00 history edited Kip CC BY-SA 4.0
updates for 10-year anniversary of this post
Jun 26, 2019 at 15:25 comment added Kip @zelusp Thanks for pointing this out. I made an edit to this answer to improve readability but ended up introducing the bug you point out in your question. I've fixed this answer, and provided an answer in your new question as well.
Jun 26, 2019 at 15:23 history edited Kip CC BY-SA 4.0
fix long-standing bug...
Jun 16, 2019 at 0:51 comment added zelusp Hey @Kip, this answer is giving me strange output... your naive function is erroneously outputting the right date for me. See here if you will.
Feb 20, 2019 at 5:11 comment added Kip @KeepMove that depends on what you need to do. 86400000 ms is equal to 24 hours, which is not always the same thing as 1 day (as I explain in the answer). So you need to decide if you are trying to add 24 hours, or add 1 day, and pick the solution that fulfills your needs.
Feb 19, 2019 at 3:01 comment added Ari Is it bad practice to do it like this? new Date(Date.now() + 86400000)
Jan 16, 2019 at 19:40 review Suggested edits
Jan 17, 2019 at 2:37
Sep 14, 2017 at 17:58 history edited Kip CC BY-SA 3.0
update moment example
Sep 13, 2017 at 15:20 comment added roneo This should be var newDateObj = moment(oldDateObj).add(30, 'm').toDate(); ( .toDate() should added to get a date object otherwise its a moment object)
Jul 24, 2017 at 15:13 history edited Kip CC BY-SA 3.0
Add url to this answer for copy/pasters
May 23, 2017 at 12:10 history edited URL Rewriter Bot
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Apr 12, 2017 at 14:52 comment added Mathieu Turcotte Thanks a lot for that Vanilla function. Such a simple function that'll save me so much time! cheers!
Mar 4, 2017 at 18:05 history edited Kip CC BY-SA 3.0
typo
Mar 4, 2017 at 18:01 comment added Kip @Blindman67 I'm using milliseconds to add hours/minutes/seconds. But how else would you add days/months/years other than using getDate()/getMonth()/getFullYear()? A day/month/year does not represent a fixed number of milliseconds.
Mar 2, 2017 at 23:16 comment added Kip @RobG Good point. I've added a fix for this and further emphasized the "use a library if possible" point.
Mar 2, 2017 at 23:16 history edited Kip CC BY-SA 3.0
bug fixes and performance improvements
Mar 1, 2017 at 5:40 comment added RobG This answer fails to accommodate month roll overs, so 31 Jan plus one month gives 2 or 3 Mar depending on whether it's a leap year or not. The addMonths and addYears functions in Jacobi's answer deal with that so for me, it's a better answer.
Mar 1, 2017 at 5:23 comment added Blindman67 Sorry my bad I read getYear.. Dont know where I saw that. Sorry.
Mar 1, 2017 at 5:22 comment added Blindman67 You have 600 upvotes and i will defer to the popular consensus. (BTW "parsed output" is what I said, you parse the native value with getDate, getMonth, & getYear (also getYear has depreciated developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…))
Mar 1, 2017 at 4:52 comment added Kip @Blindman67 that's exactly what my answer says. Then it goes on to explain the unexpected issues you will encounter if you try to use that method to add days/weeks/months/years and presents an alternative. Nowhere is a date string parsed.
Mar 1, 2017 at 3:57 comment added Blindman67 Almost 600 upvotes for a poor answer??? The correct way to add to the time object is in its native value (milliseconds since midnight January 1st 1970) Thus var timeIn30Min = new Date(new Date().valueOf() + 30 * 60 * 1000); DO NOT USE the parsed output of Date for anything other than displaying time.
Feb 27, 2017 at 12:27 comment added Vasil Popov True :) Anyway, as you also mentioned, it is much clear, nice and easy just using moment.js - var time = moment('2017-02-11 09:00:00').add(30, 'm');
Feb 20, 2017 at 19:36 comment added Kip @VasilPopov I see your point, I was just following the terminology used by the MySQL function I'm familiar with. You can always rename however you wish when you copy paste. :)
Feb 20, 2017 at 10:16 comment added Vasil Popov The dateAdd() function is great! But there is one note from me - semantically, the unit should be "minute", "second", etc. and the interval should be the amount (2, 10, 45, ...), not vice versa. Otherwise the idea is good.
Aug 4, 2016 at 17:27 history edited Kip CC BY-SA 3.0
easier-to-read example
Apr 20, 2015 at 2:33 comment added ashes999 For seconds, multiply by 1000 instead of 60k.
Sep 17, 2014 at 17:24 history edited Kip CC BY-SA 3.0
edited body
Sep 17, 2014 at 17:09 history edited Kip CC BY-SA 3.0
updated code to add hours/minutes/seconds
Sep 13, 2014 at 20:16 history edited Kip CC BY-SA 3.0
more improvement!
Apr 1, 2014 at 2:33 history edited Kip CC BY-SA 3.0
before someone nitpicks...
Jan 31, 2014 at 21:25 history edited Kip CC BY-SA 3.0
deleted 6 characters in body
Jan 31, 2014 at 21:16 history edited Kip CC BY-SA 3.0
added 658 characters in body
Jan 12, 2013 at 22:17 history edited Kip CC BY-SA 3.0
improving answer
Jul 31, 2009 at 20:36 history answered Kip CC BY-SA 2.5