1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
import type { Faker } from '.';
import type { DateEntryDefinition } from './definitions';
/**
* Converts date passed as a string, number or Date to a Date object.
* If nothing or a non parseable value is passed, takes current date.
*
* @param date Date
*/
function toDate(date?: string | Date | number): Date {
date = new Date(date);
if (isNaN(date.valueOf())) {
date = new Date();
}
return date;
}
/**
* Module to generate dates.
*/
export class _Date {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(_Date.prototype)) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}
this[name] = this[name].bind(this);
}
}
/**
* Generates a random date in the past.
*
* @param years The range of years the date may be in the past. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.recent()
*
* @example
* faker.date.past() // '2021-12-03T05:40:44.408Z'
* faker.date.past(10) // '2017-10-25T21:34:19.488Z'
* faker.date.past(10, '2020-01-01T00:00:00.000Z') // '2017-08-18T02:59:12.350Z'
*/
past(years?: number, refDate?: string | Date | number): Date {
const date = toDate(refDate);
const range = {
min: 1000,
max: (years || 1) * 365 * 24 * 3600 * 1000,
};
let past = date.getTime();
past -= this.faker.datatype.number(range); // some time from now to N years ago, in milliseconds
date.setTime(past);
return date;
}
/**
* Generates a random date in the future.
*
* @param years The range of years the date may be in the future. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.soon()
*
* @example
* faker.date.future() // '2022-11-19T05:52:49.100Z'
* faker.date.future(10) // '2030-11-23T09:38:28.710Z'
* faker.date.future(10, '2020-01-01T00:00:00.000Z') // '2020-12-13T22:45:10.252Z'
*/
future(years?: number, refDate?: string | Date | number): Date {
const date = toDate(refDate);
const range = {
min: 1000,
max: (years || 1) * 365 * 24 * 3600 * 1000,
};
let future = date.getTime();
future += this.faker.datatype.number(range); // some time from now to N years later, in milliseconds
date.setTime(future);
return date;
}
/**
* Generates a random date between the given boundaries.
*
* @param from The early date boundary.
* @param to The late date boundary.
*
* @example
* faker.date.between('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z') // '2026-05-16T02:22:53.002Z'
*/
between(from: string | Date | number, to: string | Date | number): Date {
const fromMs = toDate(from).getTime();
const toMs = toDate(to).getTime();
const dateOffset = this.faker.datatype.number(toMs - fromMs);
return new Date(fromMs + dateOffset);
}
/**
* Generates n random dates between the given boundaries.
*
* @param from The early date boundary.
* @param to The late date boundary.
* @param num The number of dates to generate. Defaults to `3`.
*
* @example
* faker.date.betweens('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z')
* // [
* // 2022-07-02T06:00:00.000Z,
* // 2024-12-31T12:00:00.000Z,
* // 2027-07-02T18:00:00.000Z
* // ]
* faker.date.betweens('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z', 2)
* // [ 2023-05-02T16:00:00.000Z, 2026-09-01T08:00:00.000Z ]
*/
betweens(
from: string | Date | number,
to: string | Date | number,
num: number = 3
): Date[] {
const dates: Date[] = [];
while (dates.length < num) {
dates.push(this.between(from, to));
}
return dates.sort((a, b) => a.getTime() - b.getTime());
}
/**
* Generates a random date in the recent past.
*
* @param days The range of days the date may be in the past. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.past()
*
* @example
* faker.date.recent() // '2022-02-04T02:09:35.077Z'
* faker.date.recent(10) // '2022-01-29T06:12:12.829Z'
* faker.date.recent(10, '2020-01-01T00:00:00.000Z') // '2019-12-27T18:11:19.117Z'
*/
recent(days?: number, refDate?: string | Date | number): Date {
const date = toDate(refDate);
const range = {
min: 1000,
max: (days || 1) * 24 * 3600 * 1000,
};
let future = date.getTime();
future -= this.faker.datatype.number(range); // some time from now to N days ago, in milliseconds
date.setTime(future);
return date;
}
/**
* Generates a random date in the near future.
*
* @param days The range of days the date may be in the future. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.future()
*
* @example
* faker.date.soon() // '2022-02-05T09:55:39.216Z'
* faker.date.soon(10) // '2022-02-11T05:14:39.138Z'
* faker.date.soon(10, '2020-01-01T00:00:00.000Z') // '2020-01-01T02:40:44.990Z'
*/
soon(days?: number, refDate?: string | Date | number): Date {
const date = toDate(refDate);
const range = {
min: 1000,
max: (days || 1) * 24 * 3600 * 1000,
};
let future = date.getTime();
future += this.faker.datatype.number(range); // some time from now to N days later, in milliseconds
date.setTime(future);
return date;
}
/**
* Returns a random name of a month.
*
* @param options The optional options to use.
* @param options.abbr Whether to return an abbreviation. Defaults to `false`.
* @param options.context Whether to return the name of a month in a context. Defaults to `false`.
*
* @example
* faker.date.month() // 'October'
* faker.date.month({ abbr: true }) // 'Feb'
* faker.date.month({ context: true }) // 'June'
* faker.date.month({ abbr: true, context: true }) // 'Sep'
*/
month(options?: { abbr?: boolean; context?: boolean }): string {
const abbr = options?.abbr ?? false;
const context = options?.context ?? false;
const source = this.faker.definitions.date.month;
let type: keyof DateEntryDefinition;
if (abbr) {
if (context && source['abbr_context'] != null) {
type = 'abbr_context';
} else {
type = 'abbr';
}
} else if (context && source['wide_context'] != null) {
type = 'wide_context';
} else {
type = 'wide';
}
return this.faker.helpers.arrayElement(source[type]);
}
/**
* Returns a random day of the week.
*
* @param options The optional options to use.
* @param options.abbr Whether to return an abbreviation. Defaults to `false`.
* @param options.context Whether to return the day of the week in a context. Defaults to `false`.
*
* @example
* faker.date.weekday() // 'Monday'
* faker.date.weekday({ abbr: true }) // 'Thu'
* faker.date.weekday({ context: true }) // 'Thursday'
* faker.date.weekday({ abbr: true, context: true }) // 'Fri'
*/
weekday(options?: { abbr?: boolean; context?: boolean }): string {
const abbr = options?.abbr ?? false;
const context = options?.context ?? false;
const source = this.faker.definitions.date.weekday;
let type: keyof DateEntryDefinition;
if (abbr) {
if (context && source['abbr_context'] != null) {
type = 'abbr_context';
} else {
type = 'abbr';
}
} else if (context && source['wide_context'] != null) {
type = 'wide_context';
} else {
type = 'wide';
}
return this.faker.helpers.arrayElement(source[type]);
}
}
|