momentJS date string add 5 days

2020-02-16 07:39发布

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.

8条回答
▲ chillily
2楼-- · 2020-02-16 08:05

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)
查看更多
女痞
3楼-- · 2020-02-16 08:23

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/

查看更多
登录 后发表回答