252

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.

Dave S
  • 3,378
  • 1
  • 20
  • 34
Dave
  • 2,815
  • 5
  • 19
  • 22

11 Answers11

513

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);
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
VtoCorleone
  • 16,813
  • 5
  • 37
  • 51
  • i want to see this format "25.03.2014" in the alert window how can i implement this ? – Dave Mar 20 '14 at 23:32
  • 3
    As of moment-2.8.4, I can see deprecated message with add('days',5). Use add(5,'days') instead. – Bala Jan 19 '16 at 07:04
  • 2
    The correct syntax : moment().add(5, 'days').format("D/M/YYYY") You can look a while here : https://momentjs.com/docs/#/manipulating/add/ – Costas Bakoulias Jul 12 '19 at 09:50
  • @Bala, Can we use add(1,'day'), if there is only one day to add ? – Aashiq May 27 '21 at 14:52
  • @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) – Bala May 29 '21 at 11:57
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.

Ben
  • 687
  • 7
  • 15
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);
Leonard Pauli
  • 2,662
  • 1
  • 23
  • 23
  • 11
    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. :-| – Gilad Barner Nov 13 '16 at 08:16
26

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/

Isidro Martínez
  • 816
  • 7
  • 15
19
var end_date = moment(start_date).clone().add(5, 'days');
MD Ashik
  • 9,117
  • 10
  • 52
  • 59
Sorin Trimbitas
  • 1,467
  • 18
  • 35
  • 3
    It is the `clone()` that really helps here – ChrisW Feb 10 '20 at 11:50
  • Thanks for saving my day!! `clone()` is really important, since `add()` mutates the original moment as the [doc](https://momentjs.com/docs/#/manipulating/add/) said. – Calios Aug 09 '21 at 05:15
16

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);

Siddharth Sunchu
  • 866
  • 10
  • 13
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.

Michel K
  • 641
  • 1
  • 6
  • 18
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)
Pytth
  • 4,008
  • 24
  • 29
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...

slfan
  • 8,950
  • 115
  • 65
  • 78
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);
Marco Barcellos
  • 196
  • 2
  • 4
1

updated:

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

alert(new_date)
borchvm
  • 3,533
  • 16
  • 44
  • 45
Nishith
  • 107
  • 1
  • 9