Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add examples and improve jsdocs for datatype.datetime #907

Merged
merged 1 commit into from
May 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/datatype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,24 @@ export class Datatype {
* Returns a Date object using a random number of milliseconds since
* the [Unix Epoch](https://en.wikipedia.org/wiki/Unix_time) (1 January 1970 UTC).
*
* @param options Max number of milliseconds since unix epoch or options object
* @param options Max number of milliseconds since unix epoch or options object.
* @param options.min Lower bound for milliseconds since base date.
* When not provided or smaller than `-8640000000000000`, `1990-01-01` is considered
* as minimum generated date. Defaults to `633880849813`.
* as minimum generated date. Defaults to `631152000000`.
* @param options.max Upper bound for milliseconds since base date.
* When not provided or larger than `8640000000000000`, `2100-01-01` is considered
* as maximum generated date.
* as maximum generated date. Defaults to `4102444800000`.
*
* @example
* faker.datatype.datetime() // '2089-04-17T18:03:24.956Z'
* faker.datatype.datetime(1893456000000) // '2022-03-28T07:00:56.876Z'
* faker.datatype.datetime({ min: 1577836800000, max: 1893456000000 }) // '2021-09-12T07:13:00.255Z'
*/
datetime(options?: number | { min?: number; max?: number }): Date {
datetime(options: number | { min?: number; max?: number } = {}): Date {
const minMax = 8640000000000000;

let min = typeof options === 'number' ? undefined : options?.min;
let max = typeof options === 'number' ? options : options?.max;
let min = typeof options === 'number' ? undefined : options.min;
let max = typeof options === 'number' ? options : options.max;

if (min == null || min < minMax * -1) {
min = Date.UTC(1990, 0);
Expand Down