How to properly add 1 month from now to current date in moment.js
172,401
Solution 1
var currentDate = moment('2015-10-30');
var futureMonth = moment(currentDate).add(1, 'M');
var futureMonthEnd = moment(futureMonth).endOf('month');
if(currentDate.date() != futureMonth.date() && futureMonth.isSame(futureMonthEnd.format('YYYY-MM-DD'))) {
futureMonth = futureMonth.add(1, 'd');
}
console.log(currentDate);
console.log(futureMonth);
EDIT
moment.addRealMonth = function addRealMonth(d) {
var fm = moment(d).add(1, 'M');
var fmEnd = moment(fm).endOf('month');
return d.date() != fm.date() && fm.isSame(fmEnd.format('YYYY-MM-DD')) ? fm.add(1, 'd') : fm;
}
var nextMonth = moment.addRealMonth(moment());
Solution 2
According to the latest doc you can do the following-
Add a day
moment().add(1, 'days').calendar();
Add Year
moment().add(1, 'years').calendar();
Add Month
moment().add(1, 'months').calendar();
Solution 3
startDate = "20.03.2020";
var newDate = moment(startDate, "DD-MM-YYYY").add(5, 'days');
console.log(newDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Solution 4
You could try
moment().add(1, 'M').subtract(1, 'day').format('DD-MM-YYYY')
Related videos on Youtube
Author by
Jack Moscovi
Updated on July 05, 2022Comments
-
Jack Moscovi almost 2 years
I read the documentation of moment.js that if you want to add 1 month from the current date time you use this code
var moment = require('moment'); var futureMonth = moment().add(1, 'M').format('DD-MM-YYYY');
But the problem right now, it is not properly add the date correctly, for example let say the current date is 31/10/2015, explain in code
var currentDate = moment().format('DD-MM-YYYY'); var futureMonth = moment().add(1, 'M').format('DD-MM-YYYY'); console.log(currentDate) // Will result --> 31/10/2015 console.log(futureMonth) // Will result --> 30/11/2015
if you take a look at the current calendar time, 1 month from
31/10/2015
supposed to be1/12/2015
Could anyone give me some opinion on how to fix this problem.
Thank you
-
Jack Moscovi over 8 yearsI got the idea from the current calendar time, if thats what you are asking for.
-
Vikram Deshmukh over 8 yearsI guess what you're looking for is
moment().add(30, "days")
. -
Jack Moscovi over 8 years@Gesper I thought moment.js define the month by itself without me needing to define it.
-
Jack Moscovi over 8 years@VikramDeshmukh I could do that, but it won't be flexible.
-
Jack Moscovi over 8 years@Jamiec Where is your answer, seems like you have taken it down?
-
Jamiec over 8 yearsWhat is the rule that you actually want? Is it "Add the number of days in the current month"? Is it "Add 1 month + 1 day"?
-
kio over 8 yearsseems like the definition in momentjs is that if you have end-of-month and add 1 month, it will do end-of-next-month. Which seems very sane and predictable to me
-
silentw over 8 years@JackMoscovi check my answer, I updated it so that it fits your needs.
-
SSH This about 7 yearsA month is not always 30 days long, so
moment().add(30, "days")
isn't technically the right answer. -
alehro almost 7 yearsPlease do not call not proper things as proper. I suggest to change title of the question somehow. Currently it is misleading.
-
Mussa over 4 yearsChange the title to "How to add a month to end of the current month?" Your expected behaviour is NOT proper and title is misleading.
-
McKay about 3 years@JackMoscovi, There has to be weird boundary conditions around month addition.
2015-10-31 + 1 month
I think should be in November.(Otherwise, it might feel like 2 months to some). If you insist that two consecutive days should not have the same "one more month day"), then I would ask you what2015-01-31 + one month
is compared to2015-02-01 + one month
should be.
-
-
Jack Moscovi over 8 yearsJust wondering is this code flexible? or its just fix the current date problem 31/10/2015 - 1/12/2015?
-
Jamiec over 8 yearsThis just goes wrong another way - July & August both have 31 days. When you add one month to July 31st you'll get 1st September.
-
Jack Moscovi over 8 yearsWow I tested on the last of January which is 31 and it works perfectly. Thanks!
-
Eike Thies over 4 yearsthis answer is wrong and does not correctly add a month according to the request above
-
Piotr Labunski about 4 yearsWhile this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
-
Tomasz Kasperczyk about 4 yearsTry
moment('2020-03-30').add(1, 'months')
and then compare withmoment('2020-03-31').add(1, 'months')
. The result will be the same, which is wrong. -
Anoop M Maddasseri about 3 years@TomaszKasperczyk Pl. report here - github.com/moment/moment/issues