Skip to content

Instantly share code, notes, and snippets.

@gabrielef
Created September 8, 2016 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielef/61519276eaaed8ed64b1da797058352b to your computer and use it in GitHub Desktop.
Save gabrielef/61519276eaaed8ed64b1da797058352b to your computer and use it in GitHub Desktop.
Retrieve the last day of the previous month
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
firstDay.setDate(firstDay.getDate()-1);
console.log(firstDay);
console.log(firstDay.getFullYear() + '-' + (firstDay.getMonth()+1) + '-' + firstDay.getDate());
//fix two char month
var month = firstDay.getMonth()+1;
if(month < 10){
month = '0' + month;
}
//fix two char date
var day = firstDay.getDate();
if(day < 10){
day = '0' + day;
}
var lastDayMonthBefore = firstDay.getFullYear() + '-' + month + '-' + day;
console.log(lastDayMonthBefore);
@gabrielef
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment