Skip to content

Instantly share code, notes, and snippets.

@neodigm
Last active August 22, 2021 03:49
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 neodigm/2813501935470d4ca351509fafb762f8 to your computer and use it in GitHub Desktop.
Save neodigm/2813501935470d4ca351509fafb762f8 to your computer and use it in GitHub Desktop.
JavaScript get first and last date of the previous month (week?)
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