Created
September 26, 2024 00:43
-
-
Save OrderAndCh4oS/00558967060e60ae1d86b9a958ec01dc to your computer and use it in GitHub Desktop.
Date Functions
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 randomInt = (max: number, min: number = 0): number => { | |
return Math.floor(Math.random() * (max - min)) + min; | |
}; | |
export const getRandomTimeToday = () => { | |
const date = new Date(); | |
date.setHours(randomInt(24)); | |
date.setMinutes(randomInt(60)); | |
date.setSeconds(randomInt(60)); | |
date.setMilliseconds(randomInt(1000)); | |
return date; | |
}; | |
export const getRandomTimeNDaysAgo = (n: number) => { | |
const date = getRandomTimeToday(); | |
date.setHours(date.getHours() - (n * 24)); | |
return date; | |
}; | |
export const getRandomTimeNDaysLater = (n: number) => { | |
const date = getRandomTimeToday(); | |
date.setHours(date.getHours() + (n * 24)); | |
return date; | |
}; | |
export const getRandomTimeBetween = (dateA: Date, dateB: Date) => { | |
return new Date(randomInt(+dateA, +dateB)); | |
}; | |
console.log(getRandomTimeNDaysAgo(30), new Date()); | |
console.log(getRandomTimeBetween(getRandomTimeNDaysAgo(30), new Date())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment