Given a date object, how to get its previous month's first day in javascript
-
a more descriptive title might be nice... – helloandre Mar 03 '09 at 04:30
-
http://stackoverflow.com/questions/594054/date-problem-in-java looks like your previous question was for java. so not exactly a duplicate :) – Gulzar Nazim Mar 03 '09 at 04:34
-
Use [moment.js](https://momentjs.com/docs/) `moment().subtract(1, "months").startOf("months").toDate()`. – Wernfried Domscheit May 10 '21 at 09:00
6 Answers
function firstDayInPreviousMonth(yourDate) {
var d = new Date(yourDate);
d.setDate(1);
d.setMonth(d.getMonth() - 1);
return d;
}
EDIT: Alright... I've definitely learned something here. I think that this is the simplest solution that covers all cases (and yes, it does work for January):
function firstDayInPreviousMonth(yourDate) {
return new Date(yourDate.getFullYear(), yourDate.getMonth() - 1, 1);
}

- 83,552
- 10
- 84
- 84
-
Note that this will FAIL for something like: firstDayInPreviousMonth(new Date("Mar 31 2009")). See the comments on: http://stackoverflow.com/questions/499838/javascript-date-next-month – Shog9 Mar 03 '09 at 04:40
-
It wouldn't fail if he setDate(1) before doing the setMonth() – Paolo Bergantino Mar 03 '09 at 04:46
-
2
-
-
@Prestaul Sorry to bring up an old post, but could you please explain how: `return new Date(yourDate.getFullYear(), yourDate.getMonth() - 1, 1)` works? I know it works, but can't get my head around as to how it handles January. If I ran it today (28/01/2014) wouldn't it return (01/00/2014)? – Chronix3 Jan 28 '14 at 01:50
-
4@Chronix3, first of all, `getMonth`, `setMonth` and the `Date` constructor use zero-based months (weird, I know) so January is represented as zero, Feb as 1, etc. The real magic here is that the js Date object is flexible enough to compensate for dates that are out of range by wrapping to next or previous months. For example, `Date(2014, 0, 31)` = Jan 31, `Date(2014, 0, 32)` = Feb 1, `Date(2014, -1, 25)` = Dec 25 2013. This is to allow for math to be performed easily. For example, I can always increment a day using `dt.setDate(dt.getDate() + 1)`. – Prestaul Jan 28 '14 at 17:09
-
Ahh I didn't know how flexible the js Date object was. Thank you so much :) – Chronix3 Jan 29 '14 at 22:52
The following should work:
now = new Date();
if (now.getMonth() == 0) {
current = new Date(now.getFullYear() - 1, 11, 1);
} else {
current = new Date(now.getFullYear(), now.getMonth() - 1, 1);
}
keeping in mind that months are zero-based so December is 11 rather than 12.
But, as others have pointed out, the month wraps, even as part of the atomic constructor, so the following is also possible:
now = new Date();
firstDayPrevMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1);

- 854,327
- 234
- 1,573
- 1,953
-
1I like this approach, but your solution actually isn't correct. When getMonth() == 1, the month is February. You want to check for getMonth() == 0, and even that isn't required because the Date constructor will accept negative values and work correctly. – Prestaul Mar 03 '09 at 04:39
-
You're neglecting the fact that setMonth will wrap values. Prestaul's answer works fine for January. – overslacked Mar 03 '09 at 04:39
-
Yes, but not for moving back to a month where the number of days is less, unfortunately. It's better to construct the new date atomically rather than piecemeal. – paxdiablo Mar 03 '09 at 04:44
-
Considering the Mar 31 example, you are correct about the anatomical date construction - if you change your answer (I do find the "blindly subtracting a month" line to be misleading given javascript's setXXX wrapping) I'll be happy to adjust my vote on this answer. – overslacked Mar 03 '09 at 04:50
-
I agree. You'll get my vote as well if you remove the test for January. – Prestaul Mar 03 '09 at 04:54
-
As you wish ... I had to check it was okay in the constructor as well, rather than just for setMonth(). – paxdiablo Mar 03 '09 at 05:00
-
+1... I love StackOverflow because the best answer usually does find its way to the top! – Prestaul Mar 03 '09 at 05:02
-
-
I like this solution. It might not be the briefest, but it highlights some functions of the setDate() method on Date() objects that not everybody will be familiar with:
function firstDayPreviousMonth(originalDate) {
var d = new Date(originalDate);
d.setDate(0); // set to last day of previous month
d.setDate(1); // set to the first day of that month
return d;
}
It makes use of the fact that .setDate(0) will change the date to point to the last day of the previous month, while .setDate(1) will change it (further) to point to the first day of that month. It lets the core Javascript libs do the heavy lifting.
You can see a working Plunk here.

- 20,856
- 14
- 77
- 117
It will help to get the previous month first and last date.
function getLastMonth(){
var now = new Date();
var lastday = new Date(now.getFullYear(), now.getMonth(), 0);
var firstday = new Date(lastday.getFullYear(), lastday.getMonth(), 1);
var lastMonth = firstday.getDate()+'/'+(firstday.getMonth()+1)+'/'+firstday.getFullYear()+' - '+lastday.getDate()+'/'+(firstday.getMonth()+1)+'/'+lastday.getFullYear();
return lastMonth;
}

- 14,054
- 10
- 41
- 74

- 253
- 8
- 17
Why reinventing the wheel?
Use moment.js or one of the alternatives (Luxon, Day.js, etc.):
moment().subtract(1, "months").startOf("months").toDate()
.

- 54,457
- 9
- 76
- 110
I've needed to use the begging of the month in a BootStrap Min month and didn't want to write it all out.
(() => new Date(new Date().getFullYear(), new Date().getMonth(),1, 0))()

- 607
- 1
- 6
- 15