Skip to content

Instantly share code, notes, and snippets.

@abdulhadad
Created May 19, 2021 12:18
Show Gist options
  • Select an option

  • Save abdulhadad/72fb90a1b6eef1aaf7ab4650cd752614 to your computer and use it in GitHub Desktop.

Select an option

Save abdulhadad/72fb90a1b6eef1aaf7ab4650cd752614 to your computer and use it in GitHub Desktop.
eOMonth_test.js
const eOMonth = (date) => (new Date(date.getFullYear(),date.getMonth()+1,0))
const formattedDate = (date) => (date.getFullYear() + '-' + String(date.getMonth() + 1).padStart(2, '0') + '-' + String(date.getDate()).padStart(2, '0'))
/// test today, prev month
const today = new Date()
const prevMonthStartDate = new Date(today.getFullYear(),today.getMonth()-1,1)
const prevMonthEndDate = new Date(today.getFullYear(),today.getMonth(),0)
console.log(formattedDate(today))
console.log(formattedDate(prevMonthStartDate))
console.log(formattedDate(prevMonthEndDate))
console.log(formattedDate(eOMonth(today)))
console.log(formattedDate(eOMonth(prevMonthStartDate)))
console.log(formattedDate(eOMonth(prevMonthEndDate)))
console.log(formattedDate(eOMonth(eOMonth(today))))
/// test new year and before
const newYear = new Date(2021, 0, 1) // january is 0
const beforeNewYear = new Date(newYear.getFullYear(),newYear.getMonth(),0)
console.log(formattedDate(newYear))
console.log(formattedDate(beforeNewYear))
console.log(formattedDate(eOMonth(newYear)))
console.log(formattedDate(eOMonth(beforeNewYear)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment