momentJS date string add 5 days
Asked Answered
A

11

258

i have a start date string "20.03.2014" and i want to add 5 days to this with moment.js but i don't get the new date "25.03.2014" in the alert window.

here my javascript Code:

startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add("DD-MM-YYYY", 5);

alert(new_date);

here my jsfiddle: http://jsfiddle.net/jbgUt/1/

How can i solve this ?

I like this string format "25.03.2014"

Hope someone can help me.

Armilda answered 20/3, 2014 at 23:15 Comment(0)
S
524

UPDATED: January 19, 2016

As of moment 2.8.4 - use .add(5, 'd') (or .add(5, 'days')) instead of .add('d', 5)

var new_date = moment(startdate, "DD-MM-YYYY").add(5, 'days');

Thanks @Bala for the information.

UPDATED: March 21, 2014

This is what you'd have to do to get that format.

Here's an updated fiddle

startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);

var day = new_date.format('DD');
var month = new_date.format('MM');
var year = new_date.format('YYYY');

alert(day + '.' + month + '.' + year);

ORIGINAL: March 20, 2014

You're not telling it how/what unit to add. Use -

 var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
Suppose answered 20/3, 2014 at 23:20 Comment(5)
i want to see this format "25.03.2014" in the alert window how can i implement this ?Armilda
As of moment-2.8.4, I can see deprecated message with add('days',5). Use add(5,'days') instead.Lanark
The correct syntax : moment().add(5, 'days').format("D/M/YYYY") You can look a while here : momentjs.com/docs/#/manipulating/addVolost
@Bala, Can we use add(1,'day'), if there is only one day to add ?Magnetism
@Ashiq, as per docs it should be either 'days' or 'd'. (Although 'day' will work as it starts with d, it should not be used)Lanark
S
35
moment(moment('2015/04/09 16:00:00').add(7, 'd').format('YYYY/MM/DD HH:mm:ss'))

has to format and then convert to moment again.

Striation answered 9/5, 2017 at 9:2 Comment(1)
You saved me lots of minutes : )Esdras
W
29

You can add days in different formats:

// Normal adding
moment().add(7, 'days');

// Short Hand
moment().add(7, 'd');

// Literal Object    
moment().add({days:7, months:1});

See more about it on Moment.js docs: https://momentjs.com/docs/#/manipulating/add/

Wideranging answered 25/4, 2017 at 21:21 Comment(0)
A
27

The function add() returns the old date, but changes the original date :)

startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
new_date.add(5, 'days');
alert(new_date);
Ainu answered 20/3, 2014 at 23:20 Comment(1)
Yeah that's weird. When checking moment(startdate).add(5, 'days') in the console you get Moment {_isAMomentObject: true, _i: "[the original date]",}, and I was sure the add method wasn't working. After spending a few minutes, I realized the method WAS working, because if you open the object you get Moment _d : [The date with the addition] _f : "YYYY-MM-DDTHH:mm:ss" _i : [The original date]... The date has actually changed (you'll get the correct result with moment(startdate).add(5, 'days').format('YYYYMMDD')), but it's confusing that it seems the date hasn't changed. :-|Venn
P
20

If we want to use the current date or present date:

var new_date = moment(moment(), "MM-DD-YYYY").add(7, 'days')
alert(new_date);

Pimpernel answered 24/8, 2021 at 0:50 Comment(0)
H
19
var end_date = moment(start_date).clone().add(5, 'days');
Hultgren answered 1/10, 2018 at 9:42 Comment(2)
It is the clone() that really helps hereImportance
Thanks for saving my day!! clone() is really important, since add() mutates the original moment as the doc said.Roquelaure
L
6

If you do end up running with formatting problems after adding X time to the function, try this format:

startDate = moment(startDate).add(1, "days").format("YYYY-MM-DD");

instead of:

startDate = moment(startDate, "YYYY-MM-DD").add(1, "days");

This last version keeps the time attached to the returned data, whereas the format method doesn't and literally returns YYYY-MM-DD.

Lurleen answered 15/7, 2021 at 8:44 Comment(0)
S
4

To get an actual working example going that returns what one would expect:

var startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
var thing = new_date.add(5, 'days').format('DD/MM/YYYY');
window.console.log(thing)
Symphony answered 24/3, 2017 at 18:44 Comment(0)
S
4
  1. add https://momentjs.com/downloads/moment-with-locales.js to your html page
  2. var todayDate = moment().format('DD-MM-YYYY');//to get today date 06/03/2018 if you want to add extra day to your current date then
  3. var dueDate = moment().add(15,'days').format('DD-MM-YYYY')// to add 15 days to current date..

point 2 and 3 are using in your jquery code...

Sungsungari answered 6/3, 2018 at 6:13 Comment(0)
T
3

You can reduce what they said in a few lines of code:

var nowPlusOneDay = moment().add('days', 1);
var nowPlusOneDayStr = nowPlusOneDay.format('YYYY-MM-DD');

alert('nowPlusOneDay Without Format(Unix Date):'+nowPlusOneDay);
alert('nowPlusOneDay Formatted(String):'+nowPlusOneDayStr);
Tensive answered 17/11, 2016 at 20:56 Comment(0)
C
1

updated:

startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add(5,'days');

alert(new_date)
Colly answered 19/3, 2020 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.