Javascript – Getting the previous month’s first date from current date in JavaScript

javascript

Please anyone share the code to find the previous month's first date from current date in JavaScript. For example, if the current date is 25th Jan 2009, I should get 1st Dec 2008 as result.

Best Solution

Straightforward enough, with the date methods:

  var x = new Date();
  x.setDate(1);
  x.setMonth(x.getMonth()-1);