Created
September 8, 2016 13:03
-
-
Save gabrielef/61519276eaaed8ed64b1da797058352b to your computer and use it in GitHub Desktop.
Retrieve the last day of the previous month
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 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://jsfiddle.net/opfxwn9p/