Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add 5 days with current date and format the output in momentjs?

I am facing problem to format the output as days/mon/years i.e ( 25/08/2019) when I add 5 days with the current date in momentjs.

console.log( moment().add(5, 'days').calendar());

Output:

Sunday at 8:30 PM

But when I add 10 days i.e:

console.log( moment().add(10, 'days').calendar());

Output:

08/30/2019

I need the output for

moment().add(5, 'days').calendar() 

as

25/08/2019

I will highly appreciate your help.

like image 880
Ben Jonson Avatar asked Dec 21 '25 04:12

Ben Jonson


2 Answers

Use format method of moment js

moment().add(5, 'days').format('DD/MM/YYYY') 
like image 114
murli2308 Avatar answered Dec 23 '25 18:12

murli2308


The moment.calendar documentation states that:

Calendar will format a date with different strings depending on how close to referenceTime's date (today by default) the date is.

You can use moment().add(5, 'days').format('DD/MM/YYYY') to achieve what you want.

If you still want to use the calendar method, we can see in the documentation that from version 2.10.5 you can pass a format parameter:

moment().add(5, 'days').calendar(null, {
    sameDay: '[Today]',
    nextDay: '[Tomorrow]',
    nextWeek: 'dddd',
    lastDay: '[Yesterday]',
    lastWeek: '[Last] dddd',
    sameElse: 'DD/MM/YYYY'
})
like image 41
N Alex Avatar answered Dec 23 '25 19:12

N Alex