We will learn how to get the first day of the previous month in JavaScript with two approaches: new Date() constructor and moment library. Let’s see the article below to know how to do it.
Get the first day of the previous month in JavaScript
Using moment.js
The moment(), startOf() and subtract() methods are included in the library moment.js. It needs to be imported inside the HTML document:
<script src = "https://momentjs.com/downloads/moment.js"> </script>
Syntax:
startOf(unit)
subtract(number, unit)
Parameters:
- unit: A string represents the kind of time, such as “year” or “month”.
- number: The number of months to be subtracted, in our case, is 1.
You can use the moment()
constructor to create a moment object which represents the current day. Then you use the startOf(“month”)
method to get the start day of this month (which is the 1st date of the current month). Finally you use the subtract method to get the first day of the previous month:
<html> <body> <script src = "https://momentjs.com/downloads/moment.js"> </script> <script type="text/javascript"> // Get the first day of the current month console.log(moment().startOf("month")) // Then get the first day of the previous month document.body.innerText = moment().startOf("months").subtract(1, "month") </script> </body> </html>
Output:
Sat Oct 01 2022 00:00:00 GMT+0700
We have provided you with two commands. The first one is to get the first day of the current month and print it in the console. While the second one is shown in the HTML body. However, if you don’t want the code to automatically update the month for you in the future, you can refer to the static month example instead:
<html> <body> <script src = "https://momentjs.com/downloads/moment.js"> </script> <script type="text/javascript"> // Get the first day of the previous month (supposed this month is Sep 2022) document.body.innerText = moment("LearnShareIT debut in Sep 2022").subtract(1, "month") </script> </body> </html>
Output:
Mon Aug 01 2022 00:00:00 GMT+0700
The example above indicates that this month is April 2022 and we get the first day of the previous month using the moment constructor without the startOf() method. As you can see, using this constructor is not difficult as it allows us to describe the month you want to generate, then we use the subtract() method to go back 1 month from the time we created. In addition, if you are working with Date objects in JavaScript, you can also convert the moment object into Date one using the toDate() method:
<html> <body> <script src = "https://momentjs.com/downloads/moment.js"> </script> <script type="text/javascript"> // Get the date object of the first day of the previous month (supposed this month is Sep 2022) let dateObject = moment("LearnShareIT debut in Sep 2022").subtract(1, "month").toDate() document.body.innerText = dateObject.constructor.name + dateObject </script> </body> </html>
Output:
DateMon Aug 01 2022 00:00:00 GMT+0700 (Giờ Đông Dương)
In the following section, we will give you another method that doesn’t require an external library like this one.
Using new Date() constructor
The new Date() constructor returns a date object that represents the first day of the month in the year you have specified.
Syntax:
new Date(year, monthIndex)
Parameters:
- year: A number represents the desired year of the date.
- monthIndex: A number represents the desired month starting from 0th for January.
You can get the first day of a month, for example, 2022, by specifying the year is 2022, monthIndex being the current month number minus 1:
// Get the first day of a year let first = new Date(2022,10); console.log(first)
Output:
Tue Nov 01 2022 00:00:00 GMT+0700
The example above points out that the current month is November, which you may refer to as 11. But the monthIndex is different as it starts from 0th, so you must minus 11 with 1 to get the correct index. In case you don’t know what the year and the month is, you can use the method getYear() and getMonth() on a new Date object.
// Get the current date let date = new Date() // Get the current year let year = date.getFullYear() // Get the current monthIndex let monthIndex = date.getMonth() // Get the first day of this month let first = new Date(year,monthIndex); console.log(first)
Output:
Tue Nov 01 2022 00:00:00 GMT+0700
Finally, you can get the first day of the previous month by minus the monthIndex with 1, because it will return the monthIndex of the previous month, and you can create a new Date object with these values:
// Get the current date let date = new Date() // Get the current year let year = date.getFullYear() // Get the current monthIndex let monthIndex = date.getMonth() // Get the first day of the previous month let first = new Date(year,monthIndex-1); console.log(first)
Output:
Sat Oct 01 2022 00:00:00 GMT+0700
As can be seen, both the moment.js or the Date() constructor allow you to do the task. However, you shouldn’t pass a string to the Date() constructor as it must follow a date string format. Otherwise, the string “Invalid Date” will be returned instead of a date object or an error.
let learnShareIt = new Date("You cannot pass this whole string to this constructor to get 2022"); console.log(learnShareIt)
Output:
Invalid Date
Summary
We have learned to get the first day of the previous month in JavaScript. We suggest you use the new Date() constructor method as it uses built-in methods. Feel free to leave us a reply to improve our tutorials.
Maybe you are interested:
- Get the First Day of a Year using JavaScript
- Get the First Day of Next Month in JavaScript
- Get the First Day of a Month in JavaScript

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R