Find first day of previous month in javascript

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

Leave a Comment