Created
March 12, 2015 22:11
-
-
Save bernbecht/d1a8ae38d520a52f61e0 to your computer and use it in GitHub Desktop.
Get -30 days from a date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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