Faker.js: faker.date.past(N) always return random date

Created on 17 Jan 2019  路  4Comments  路  Source: Marak/faker.js

So, I look the documentation and it said:

Past faker.date.past(years, refDate) Returns a date from a reference date to N years ago.

When I try to execute it, it's always getting random

const faker = require('faker')

const N = 10;
console.log(faker.date.past(N)); // return 2016-02-28T04:46:00.384Z
console.log(faker.date.past(N)) // return 2013-08-02T03:41:39.812Z

console.log(faker.date.past(N, new Date()) //return 2015-02-07T14:40:15.780Z
console.log(faker.date.past(N, new Date()) //return 2014-03-29T01:59:31.280Z

Should it return to N years ago?

Most helpful comment

I think you misunderstand, but I'm not sure I understand what you expect it to return. faker.date.past(years, refDate) will return a randomly generated Date objects up to years years before refDate, which defaults to new Date().

So for example, to get a random date up to 1 year in the past:

> faker.date.past(1)
2018-06-02T20:24:32.338Z

Or to get a random date up to 10 years before January 1, 2001:

> faker.date.past(10, new Date(2001, 0, 1))
1995-06-13T23:44:41.043Z

All 4 comments

I think you misunderstand, but I'm not sure I understand what you expect it to return. faker.date.past(years, refDate) will return a randomly generated Date objects up to years years before refDate, which defaults to new Date().

So for example, to get a random date up to 1 year in the past:

> faker.date.past(1)
2018-06-02T20:24:32.338Z

Or to get a random date up to 10 years before January 1, 2001:

> faker.date.past(10, new Date(2001, 0, 1))
1995-06-13T23:44:41.043Z

Thanks for the response.
That's why I'm asking why it return N random years ago, tested just now
Faker 4.1.0

image

Oh, I think I got the point.
I think the documentation should write

Returns a date between a reference date and N years ago

instead

Returns a date from a reference date to N years ago

I think I will close this issue

Was this page helpful?
0 / 5 - 0 ratings