Last active
August 22, 2021 03:49
-
-
Save neodigm/2813501935470d4ca351509fafb762f8 to your computer and use it in GitHub Desktop.
JavaScript get first and last date of the previous month (week?)
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
function previousMonth(originalDate) { | |
let f = new Date(originalDate) | |
let l = new Date(originalDate) | |
l.setDate(0) // set to last day of previous month | |
f.setDate(0) | |
f.setDate(1) // set to the first day of that month | |
return [f, l] | |
} | |
function previousWeek(originalDate) { | |
let f = new Date(originalDate) | |
let l = new Date(originalDate) | |
// set to Monday of this week | |
f.setDate(f.getDate() - (f.getDay() + 6) % 7); | |
f.setDate(f.getDate() - 8); | |
l.setDate(f.getDate() + 6); | |
return [f, l] | |
} | |
dtFormatShort( nDate ) { // MM/DD/YYYY | |
let dDate = new Date( nDate * 1000 ) | |
let year = dDate.getFullYear() | |
let month = (1 + dDate.getMonth()).toString() | |
let day = dDate.getDate().toString() | |
month = month.length > 1 ? month : '0' + month | |
day = day.length > 1 ? day : '0' + day | |
return month + '/' + day + '/' + year | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment