Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get last day of the month? #242

Closed
mb-dev opened this issue Mar 30, 2012 · 12 comments
Closed

Get last day of the month? #242

mb-dev opened this issue Mar 30, 2012 · 12 comments

Comments

@mb-dev
Copy link

@mb-dev mb-dev commented Mar 30, 2012

Will be great to be able to get/set moment instance to last day of the month - similar to eod.

@mb-dev
Copy link
Author

@mb-dev mb-dev commented Mar 30, 2012

I guess for now this works:
moment().add('months', 1).date(1).subtract('days', 1)

@timrwood
Copy link
Member

@timrwood timrwood commented Mar 31, 2012

Also, the setters overflow if they are out of range.

moment().date(0); // the last day of last month
moment().date(-1); // the second to last day of last month
moment().hours(25); // 1 am the next day

So you could do something like this...

moment().add('months', 1).date(0);

@rwrede
Copy link

@rwrede rwrede commented Mar 24, 2014

moment().endOf('month');

works since 1.7

@lance-ding
Copy link

@lance-ding lance-ding commented Mar 28, 2014

moment().endOf('month');

or

moment(year-month-day).endOf('month');

@andyfleming
Copy link

@andyfleming andyfleming commented Oct 27, 2014

@lance-ding Thanks! That's was definitely helpful.

I still think it would be nice to have a .format() option for the number of days in the current month.

@cerkit
Copy link

@cerkit cerkit commented Mar 25, 2016

Related: you can also use moment().daysInMonth() to get an integer representing the last day in the month.

@gideonapollob
Copy link

@gideonapollob gideonapollob commented Apr 6, 2016

Related:

Deprecation warning:
moment().add(period, number) is deprecated. Please use moment().add(number, period).

So a little change to the code would be: moment().add(1, 'months').date(1).subtract('days', 1)

@maggiepint
Copy link
Member

@maggiepint maggiepint commented Apr 6, 2016

@gideonapollob , yep. You would need to reverse in your .subtract() as well.

But .endOf() is almost for sure the best choice for what you're doing.

@holic158
Copy link

@holic158 holic158 commented Oct 19, 2017

You may get last day of month. like this.

moment(2017-10).endOf('month').format('DD')

@jojo-joseph
Copy link

@jojo-joseph jojo-joseph commented Aug 21, 2019

which shows last month last date

moment().subtract(1, 'months').endOf('month').format('DD-MM-YYYY')

@pramodjb
Copy link

@pramodjb pramodjb commented Nov 25, 2020

Related: you can also use moment().daysInMonth() to get an integer representing the last day in the month.

here last day 31 wont getting

@cerkit
Copy link

@cerkit cerkit commented Nov 25, 2020

Related: you can also use moment().daysInMonth() to get an integer representing the last day in the month.

here last day 31 wont getting

Without context, it's hard to know which month you're trying to get a result for, but there are only 30 days in November, the current month.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet