简体   繁体   中英

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?

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/

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

You want to use date() instead

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

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

它返回日期,您可以将其解析为整数

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM