How to use faker.date.between faker.js

20,762

Solution 1

Their code from Github shows:

self.between = function (from, to) {
  var fromMilli = Date.parse(from);
  var dateOffset = faker.random.number(Date.parse(to) - fromMilli);

  var newDate = new Date(fromMilli + dateOffset);

  return newDate;
};

And I don't know how you're using it, or why it's not working on their example.... But this should give you some direction, at least.

If this does not help you, or is still producing an undesired result, I would open an issue up on their github.

Solution 2

Not sure when did they update wiki, but they do have a working sample for date.between now

faker.date.between('2015-01-01', '2015-01-05');
Share:
20,762
Hamza
Author by

Hamza

Updated on July 09, 2022

Comments

  • Hamza
    Hamza almost 2 years

    I want to generate some dates between two fixed values, but I don't know how to

    use faker.date.between to achieve that.

    the example in faker js demo is just giving a null value.

  • Corin
    Corin about 2 years
    For those who end up here and wonder about the wiki link, try the official documentation at fakerjs.dev/api/date.html#between