Skip to content

Instantly share code, notes, and snippets.

@bernbecht
Created March 12, 2015 22:11
Show Gist options
  • Save bernbecht/d1a8ae38d520a52f61e0 to your computer and use it in GitHub Desktop.
Save bernbecht/d1a8ae38d520a52f61e0 to your computer and use it in GitHub Desktop.
Get -30 days from a date
var actualDate = new Date(2009,02,31);
var date = actualDate.getDate().toString();
var month = actualDate.getMonth().toString();
var year = actualDate.getFullYear().toString();
var testDate = new Date(year, month, date);
console.log("Antes");
console.log(actualDate);
console.log(testDate);
testDate.setDate(actualDate.getDate()+1);
if(testDate.getMonth() !== actualDate.getMonth()){
console.log("Ultimo dia");
var lastMonth = new Date(year, month, 1);
lastMonth.setMonth(lastMonth.getMonth());
lastMonth.setDate(0);
}
else{
console.log("Nem é");
var lastMonth = new Date(year, month, date);
lastMonth.setMonth(lastMonth.getMonth()-1);
}
console.log("Depois");
console.log(actualDate);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment