Last active
October 9, 2024 22:10
-
-
Save miguelmota/5b67e03845d840c949c4 to your computer and use it in GitHub Desktop.
Random date in JavaScript
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 randomDate(start, end) { | |
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())) | |
} | |
console.log(randomDate(new Date(2012, 0, 1), new Date())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@DidarHalla I am not a JS dev but I think that it works like this:
Then this is the order of actions:
(I do not know if it can be equal end.getTime() because I do not know the "scale" of roundings)
Btw. I know that your comment is almost 2 years old but maybe it will be useful for someone else or for you if you gave up the topic :D