- Instant help with your JavaScript coding problems

Generate random date with JavaScript

How to generate random date with JavaScript?
function getRandomDate() {
    const maxDate = Date.now();
    const timestamp = Math.floor(Math.random() * maxDate);
    return new Date(timestamp);


To generate a random date you can use the Math.random function and the fact that a Date can be created from a timestamp that is an integer number of milliseconds elapsed since January 1, 1970 00:00:00 UTC

You need to set a max value, for example, the actual date's timestamp, and use it to generate a new, random number. With this new timestamp value, you can create a new Date object using it as a constructor parameter.

Note: This solution generates random dates only from the date January 1, 1970.


Share "How to generate random date with JavaScript?"