Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.day() returns wrong day of month with Moment.js

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?

like image 987
Yaron Levi Avatar asked Feb 02 '15 17:02

Yaron Levi


People also ask

How do you find the day of the month in a moment?

The moment(). daysInMonth() function is used to get the number of days in month of a particular month in Node.

How do you find the day from date in a moment?

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.

Why you shouldnt use MomentJS?

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.

What is Moment () in JavaScript?

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.


1 Answers

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.

like image 107
David Sherret Avatar answered Sep 22 '22 23:09

David Sherret