1 | # date
|
2 |
|
3 | ```js
|
4 | // usage
|
5 | chance.date()
|
6 | chance.date({string: true})
|
7 | chance.date({string: true, american: false})
|
8 | chance.date({year: 1983})
|
9 | ```
|
10 |
|
11 | Generate a random date
|
12 |
|
13 | ```js
|
14 | chance.date();
|
15 | => Sat Apr 09 2072 00:00:00 GMT-0400 (EDT)
|
16 | ```
|
17 |
|
18 | By default, returns an actual [Date][Date] object
|
19 |
|
20 | Can optionally specify that a date be returned as a string
|
21 |
|
22 | ```js
|
23 | chance.date({string: true});
|
24 | => "5/27/2078"
|
25 | ```
|
26 |
|
27 | This will return a date string of the format MM/DD/YYYY.
|
28 |
|
29 | Now of course MM/DD/YYYY is the "American" date method, but it's the default
|
30 | because there isn't much support for internationalization here yet. Further,
|
31 | it's the format used by [Facebook][FB] and other services for birthdays and
|
32 | other non-Date object dates.
|
33 |
|
34 | However, we support returning dates in DD/MM/YYYY format as well when requesting
|
35 | a date by a string and passing `american: false`.
|
36 |
|
37 | ```js
|
38 | chance.date({string: true, american: false});
|
39 | => "13/2/2017"
|
40 | ```
|
41 |
|
42 | If you want richer control over date format, strongly suggest using the
|
43 | [Moment][Moment] library. Our formatting is very minimalist, and it's out of our
|
44 | core competency to offer dates in a myriad of formats.
|
45 |
|
46 | Can optionally specify defaults for any of day, month, or year.
|
47 |
|
48 | ```js
|
49 | chance.date({year: 1983});
|
50 | => Wed May 04 1983 00:00:00 GMT-0400 (EDT)
|
51 |
|
52 | chance.date({month: 0});
|
53 | => Tue Jan 18 2084 00:00:00 GMT-0500 (EST)
|
54 |
|
55 | chance.date({day: 21});
|
56 | => Sun Oct 21 2103 00:00:00 GMT-0400 (EDT)
|
57 | ```
|
58 |
|
59 | A random date is generated, but the default you specify is kept constant.
|
60 |
|
61 | Note, month is 0-indexed. This is a carryover from the core JavaScript
|
62 | [Date][Date] object which we use internally to generate the date. We
|
63 | considered
|
64 |
|
65 | [Date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
|
66 | [FB]: https://developers.facebook.com/docs/reference/api/user/
|
67 | [Moment]: http://momentjs.com
|