Last active
August 9, 2021 07:14
-
-
Save dbwodlf3/4a0f6794af4c562163da9e3e05473eac to your computer and use it in GitHub Desktop.
js short snippet related date
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
| function getDateString(inputDate: Date) { | |
| const date = inputDate; | |
| return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`; | |
| } | |
| function getCurrentDate() { | |
| const date = new Date(); | |
| return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`; | |
| } | |
| function getCurrentDateGap(day: number) { | |
| const date = new Date(new Date().getTime() - day * 86400000); | |
| return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`; | |
| } | |
| function getWeekFirstDay(inputDate: Date) { | |
| const day = inputDate.getDay(); | |
| const dayUnit = 1000 * 60 * 60 * 24; | |
| return new Date(inputDate.getTime() - dayUnit * day); | |
| } | |
| function getWeekLastDay(inputDate: Date) { | |
| const day = inputDate.getDay(); | |
| const dayUnit = 1000 * 60 * 60 * 24; | |
| return new Date(inputDate.getTime() + dayUnit * (6 - day)); | |
| } | |
| function getMonthFirstDay(inputDate: Date) { | |
| const year = inputDate.getFullYear(); | |
| const month = inputDate.getMonth(); | |
| return new Date(year, month, 1); | |
| } | |
| function getMonthLastDay(inputDate: Date) { | |
| const year = inputDate.getFullYear(); | |
| const month = inputDate.getMonth(); | |
| return new Date(year, month + 1, 0); | |
| } | |
| function getYearFirstDay(inputDate: Date) { | |
| const year = inputDate.getFullYear(); | |
| return new Date(year, 1, 1); | |
| } | |
| function getYearLastDay(inputDate: Date) { | |
| const year = inputDate.getFullYear(); | |
| return new Date(year, 1, 0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment