Skip to content

Instantly share code, notes, and snippets.

@imbharat420
Last active March 11, 2023 13:27
Show Gist options
  • Save imbharat420/919409c99c26a3c0e690f231caf3c7a4 to your computer and use it in GitHub Desktop.
Save imbharat420/919409c99c26a3c0e690f231caf3c7a4 to your computer and use it in GitHub Desktop.
Date Checker implementation for basic use cases
//date format: 1678452212622
const DateChecker = (date) => {
const then = new Date(date);
const [day, month, year] = [then.getDate(), then.getMonth(), then.getFullYear()];
const [hour, minute, second] = [then.getHours(), then.getMinutes(), then.getSeconds()];
return {
getDay: () => day,
getMonth: () => month,
getYear: () => year,
getHour: () => hour,
getMinute: () => minute,
getSecond: () => second,
getThen: () => ({
hour,
minute,
second,
day,
month,
year
}),
isGap: (time) => {
// time format: 1s 30m, 1h, 1d, 1y
const [timeNumber, timeType] = [time.slice(0, time.length - 1), time.slice(time.length - 1)];
const now = new Date();
const gapInSeconds = Math.round((now.getTime() - then.getTime()) / 1000);
const gapInMinutes = Math.round(gapInSeconds / 60);
const gapInHours = Math.round(gapInMinutes / 60);
const gapInDays = Math.round(gapInHours / 24);
const gapInYears = Math.round(gapInDays / 365);
if (timeType === 's') {
return gapInSeconds >= +timeNumber;
}
if (timeType === 'm') {
return gapInMinutes >= +timeNumber;
}
if (timeType === 'h') {
return gapInHours >= +timeNumber;
}
if (timeType === 'd') {
return gapInDays >= +timeNumber;
}
if (timeType === 'y') {
return gapInYears >= +timeNumber;
}
return false;
},
difference: (fromTime) => {
const now = new Date(+fromTime);
const gapInSeconds = Math.round((now.getTime() - then.getTime()) / 1000);
const gapInMinutes = Math.round(gapInSeconds / 60);
const gapInHours = Math.round(gapInMinutes / 60);
const gapInDays = Math.round(gapInHours / 24);
const gapInYears = Math.round(gapInDays / 365);
return {
seconds: gapInSeconds,
minutes: gapInMinutes,
hours: gapInHours,
days: gapInDays,
years: gapInYears
}
},
isSameMinute: (time) => {
const now = new Date(+time);
const [day2, month2, year2] = [now.getDate(), now.getMonth(), now.getFullYear()];
return day === day2 && month === month2 && year === year2 && then.getMinutes() === now.getMinutes();
},
isSameHour: (time) => {
const now = new Date(+time);
const [day2, month2, year2] = [now.getDate(), now.getMonth(), now.getFullYear()];
return day === day2 && month === month2 && year === year2 && then.getHours() === now.getHours();
},
isSameDay: (time) => {
const now = new Date(+time);
const [day2, month2, year2] = [now.getDate(), now.getMonth(), now.getFullYear()];
return day === day2 && month === month2 && year === year2;
},
isSameYear: (time) => {
const now = new Date(+time);
const [, , year2] = [now.getDate(), now.getMonth(), now.getFullYear()];
return year === year2;
},
};
};
const date1 = new Date()
let getThen = DateChecker(1678459080192).getThen()
let isGap = DateChecker(1678459080192).isGap("20h")
let isSameMinute = DateChecker(1678459080192).isSameMinute(1678540563421)
let isSameHour = DateChecker(1678459080192).isSameHour(1678540563421)
let isSameDay = DateChecker(1678459080192).isSameDay(1678540563421)
let isSameYear = DateChecker(1678459080192).isSameYear(1678540563421)
let difference = DateChecker(1678459080192).difference(1678540563421)
let getDay = DateChecker(1678459080192).getDay(1678540563421)
let getSecond = DateChecker(1678459080192).getSecond(1678540563421)
let getMinute = DateChecker(1678459080192).getMinute(1678540563421)
let getHour = DateChecker(1678459080192).getHour(1678540563421)
let getMonth = DateChecker(1678459080192).getMonth(1678540563421)
let getYear = DateChecker(1678459080192).getYear(1678540563421)
console.log({
getThen,
isGap,
isSameMinute,
isSameHour,
isSameDay,
isSameYear,
difference,
getDay,
getSecond,
getMinute,
getHour,
getMonth,
getYear
})
/*
//Output
{
getThen: {
hour: 14,
minute: 38,
second: 0,
day: 10,
month: 2,
year: 2023
},
isGap: true,
isSameMinute: false,
isSameHour: false,
isSameDay: false,
isSameYear: true,
difference: {
seconds: 81483,
minutes: 1358,
hours: 23,
days: 1,
years: 0
},
getDay: 10,
getSecond: 0,
getMinute: 38,
getHour: 14,
getMonth: 2,
getYear: 2023
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment