Frnak
Frnak

Reputation: 6812

Moment.js returns 1 as last day of month

I'm trying to get the last day of the month as an integer. So for the month december 31 should be returned.

moment().endOf('month').day()

This will, however, return 1 - even though

moment().endOf('month').format('YYYY-MM-DD')

returns the correct date. Can someone explain why and how to get the actual day?

Upvotes: 0

Views: 1302

Answers (5)

Manikandan
Manikandan

Reputation: 202

var lastDate = moment().endOf('month').format('DD') 

which returns the day and you can parseInt this for making it as integer

Upvotes: 0

Fida
Fida

Reputation: 1358

moment().endOf('month').format('DD')

also

moment().endOf('month').date();

Upvotes: 1

galchen
galchen

Reputation: 5290

day() returns the day of the week, not the date

You want to use date() instead

Upvotes: 1

Feathercrown
Feathercrown

Reputation: 2591

As galchen said, day() returns the day of the week. To get the date, manipulate the string:

var lastDay = moment().endOf('month').format('YYYY-MM-DD').split('-')[2];

Upvotes: -1

Daniel Diekmeier
Daniel Diekmeier

Reputation: 3434

To get the day of the month, you have to use .date() instead. See the relevant part of the docs: http://momentjs.com/docs/#/get-set/date/

Upvotes: 5

Related Questions