Created
May 19, 2021 12:18
-
-
Save abdulhadad/72fb90a1b6eef1aaf7ab4650cd752614 to your computer and use it in GitHub Desktop.
eOMonth_test.js
This file contains hidden or 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
| 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