I am using Moment.js to parse a string and get the day, month and year separately:
var date = moment("12-25-1995", "MM-DD-YYYY"); var day = date.day();
However, day
is not 25—it's 1. What is the correct API method?
The moment(). daysInMonth() function is used to get the number of days in month of a particular month in Node.
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). format("dddd"); console.
However, Moment. js has too many drawbacks compared to modern date and time libraries. Its API is not immutable, it is large and it doesn't support tree shaking. Even the Moment team discourages to use their library in new projects.
MomentJS is a JavaScript library which helps is parsing, validating, manipulating and displaying date/time in JavaScript in a very easy way. This chapter will provide an overview of MomentJS and discusses its features in detail. Moment JS allows displaying of date as per localization and in human readable format.
The correct function to use is .date()
:
date.date() === 25;
.day()
gives you the day of the week. This works similarly to javascript's .getDate()
and .getDay()
functions on the date object.
If you want to get the month and year, you can use the .month()
and .year()
functions.
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