Pregunta

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.

¿Fue útil?

Solución

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

Otros consejos

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.

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

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/

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

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

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.

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

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

updated:

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

alert(new_date)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top