momentJS date string add 5 days

JavascriptMomentjsAddDays

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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.

Solution 3 - Javascript

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

Solution 4 - Javascript

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/

Solution 5 - Javascript

var end_date = moment(start_date).clone().add(5, 'days');

Solution 6 - Javascript

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

Solution 7 - Javascript

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)

Solution 8 - Javascript

  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...

Solution 9 - Javascript

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

Solution 10 - Javascript

updated:

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

alert(new_date)

Solution 11 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaveView Question on Stackoverflow
Solution 1 - JavascriptVtoCorleoneView Answer on Stackoverflow
Solution 2 - JavascriptBenView Answer on Stackoverflow
Solution 3 - JavascriptLeonard PauliView Answer on Stackoverflow
Solution 4 - JavascriptIsidro MartínezView Answer on Stackoverflow
Solution 5 - JavascriptSorin TrimbitasView Answer on Stackoverflow
Solution 6 - JavascriptSiddharth SunchuView Answer on Stackoverflow
Solution 7 - JavascriptPytthView Answer on Stackoverflow
Solution 8 - JavascriptKamala kanta dasView Answer on Stackoverflow
Solution 9 - JavascriptMarco BarcellosView Answer on Stackoverflow
Solution 10 - JavascriptNishithView Answer on Stackoverflow
Solution 11 - JavascriptMichel KView Answer on Stackoverflow