How would one use moment.js to get the number of days in the current month? Preferably without temporary variables.
Use the Moment.We call moment to create a Moment object with the current date and time. Then we call clone to clone that object. Then we call startOf with 'month' to return the first day of the current month. And then we call format to format the date into the human-readable YYYY-MM-DD format.
To get day name from date with Moment. js and JavaScript, we can use the format method. const myDate = "2022-06-28T00:00:00"; const weekDayName = moment(myDate).
The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().
Moment has a daysInMonth
function:
Days in Month 1.5.0+
moment().daysInMonth();
Get the number of days in the current month.
moment("2012-02", "YYYY-MM").daysInMonth() // 29 moment("2012-01", "YYYY-MM").daysInMonth() // 31
You can get the days in an array
Array.from(Array(moment('2020-02').daysInMonth()).keys()) //=> [0, 1, 2, 3, 4, 5...27, 28] Array.from(Array(moment('2020-02').daysInMonth()), (_, i) => i + 1) //=> [1, 2, 3, 4, 5...28, 29]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With