简体   繁体   English

设置“max”和“min”时,如何从fakerjs date.birthdate() 中获得4位数的年份

[英]How do I get a 4-digit year from fakerjs date.birthdate() when setting `max` and `min`

I'm trying to use the community fakerjs library to get a random birthdate .我正在尝试使用社区 fakerjs 库来获取随机生日 I want it to be within the last 70 years at the latest.我希望它最迟在过去 70 年之内。 In my output, though, I keep getting years like 79 and 12 instead of 1979 or 2012 .不过,在我的输出中,我不断得到像7912这样的年份,而不是19792012 I can only get a 2-digit year.我只能得到 2 位数的年份。

let date = faker.date.birthdate({ max: 70, min: 1 });
// 0037-08-27T13:35:09.312Z
date.getFullYear()
// 37

.birthdate() by itself works fine, it's just when I use max and min that the problem shows up. .birthdate()本身工作正常,只是当我使用maxmin时才会出现问题。

What am I doing wrong?我究竟做错了什么?

There's no bug, I just wasn't reading the docs correctly.没有错误,我只是没有正确阅读文档。 I assumed faker.date.birthdate({ max: 70, min: 1 }) would understand 70 and 1 to be ages, but the method by default thinks they're actually years, so it's imagining that I want anything between the year 0001 and the year 0070 .我假设faker.date.birthdate({ max: 70, min: 1 })会理解701是年龄,但默认情况下该方法认为它们实际上是年,所以它想象我想要0001年之间的任何东西和0070年。

I needed to add mode: 'age' to the options:我需要将mode: 'age'添加到选项中:

let date = faker.date.birthdate({ max: 70, min: 1, mode: 'age' });
// 1965-02-05T20:58:06.207Z
date.getFullYear()
// 1965

By default, mode is set to year .默认情况下, mode设置为year

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM