Faker.js: Seed does not work for date

Created on 23 Feb 2018  Â·  2Comments  Â·  Source: Marak/faker.js

> const f = require('faker');
> f.seed(123)
undefined
> f.date.past()
2017-06-13T21:30:19.831Z
> f.seed(123)
undefined
> f.date.past()
2017-06-13T21:30:22.503Z

Most helpful comment

I noticed this issue then had a look at the source and turns out that you'll only get the same date if you supply a refDate parameter, which comes after the years parameter.

So you can do f.date.past(1, new Date(2018, 0, 0)) to get a date in 2017† that depends only on the seed.

† You might get the last second of 2016 or something. I haven't checked.

All 2 comments

I noticed this issue then had a look at the source and turns out that you'll only get the same date if you supply a refDate parameter, which comes after the years parameter.

So you can do f.date.past(1, new Date(2018, 0, 0)) to get a date in 2017† that depends only on the seed.

† You might get the last second of 2016 or something. I haven't checked.

I ran into this issue and ended up monkeypatching the date-related methods to provide a consistent refDate when needed:

const oldPast = faker.date.past;
faker.date.past = (years = 1, refDate = new Date(2018, 0, 0)) =>
  oldPast.call(faker.date, years, refDate);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

JeffBeltran picture JeffBeltran  Â·  5Comments

developdeez picture developdeez  Â·  5Comments

HoustonBass picture HoustonBass  Â·  6Comments

kevinburke picture kevinburke  Â·  5Comments

Aleyasen picture Aleyasen  Â·  6Comments