Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find first day of previous month in javascript

Tags:

javascript

People also ask

How to get last month first and last date in JavaScript?

Copied! const date = new Date('2022-01-17'); const firstDayPrevMonth = new Date(date. getFullYear(), date. getMonth() - 1, 1); // 👇️ Wed Dec 01 2021 00:00:00 console.

How do you get the first date of the month in typescript?

To get the first and last day of the current month, use the getFullYear() and getMonth() methods to get the current year and month and pass them to the Date() constructor to get an object representing the two dates. Copied! const now = new Date(); const firstDay = new Date(now. getFullYear(), now.

How can I get previous month in moment?

To get the previous month in moment js, use the subtract(1, "month") method to subtract the month in date and use format('MMMM') to get the month name from subtracted date. Just import moment in your file and call moment(). subtract(1, "month"). format('MMMM') and it will return previous month.


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);
}

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);

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.


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;
    }

Why reinventing the wheel?

Use moment.js or one of the alternatives (Luxon, Day.js, etc.):

moment().subtract(1, "months").startOf("months").toDate().