Skip to content

Instantly share code, notes, and snippets.

@dbwodlf3
Last active August 9, 2021 07:14
Show Gist options
  • Select an option

  • Save dbwodlf3/4a0f6794af4c562163da9e3e05473eac to your computer and use it in GitHub Desktop.

Select an option

Save dbwodlf3/4a0f6794af4c562163da9e3e05473eac to your computer and use it in GitHub Desktop.
js short snippet related date
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