#399·chancejs

Seeded date generator shifts one year at new year's

Author: papandreouCreated Jan 7, 2019Updated Jan 21, 2026

Hello!

I have a test suite that depends on an exact sequence of dates being generated. I was surprised to see that it failed when I ran it today. Turns out that Chance#date will use Chance#year when no min/max bounds are specified: https://github.com/chancejs/chancejs/blob/d16e93c8be17a7da5a8226acd561c78121e3960a/chance.js#L1857

... and Chance#year will (when not bounded) generate a year between the current year and 100 years in the future: https://github.com/chancejs/chancejs/blob/d16e93c8be17a7da5a8226acd561c78121e3960a/chance.js#L1951

It can be boiled down to:

javascript
const Chance = require('chance');
const chance = new Chance(42);
console.log(chance.date());

In 2019 this script outputs 2099-05-30T02:43:46.598Z every time it's run, but if I dial back my computer's clock to December 31st, 2018, it will output a date exactly one year earlier: 2098-05-30T02:43:46.598Z. And so forth with earlier years.

When I read the source code I get the feeling that this is sort of by design, or at least that it's a feature that date and year can be smart and pick a "realistic" value range determined by the current year. But it strikes me as a bad/surprising default. There are many use cases for chance.js where it's important to get a reproducible value sequence.

I'd like to help out with a fix, but I'd like to check if there's consensus that it's a bug first ;)