Find first day of previous month in javascript

Given a date object, how to get its previous month's first day 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);
}

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