UNPKG

1.83 kBMarkdownView Raw
1# date
2
3```js
4// usage
5chance.date()
6chance.date({string: true})
7chance.date({string: true, american: false})
8chance.date({year: 1983})
9```
10
11Generate a random date
12
13```js
14chance.date();
15=> Sat Apr 09 2072 00:00:00 GMT-0400 (EDT)
16```
17
18By default, returns an actual [Date][Date] object
19
20Can optionally specify that a date be returned as a string
21
22```js
23chance.date({string: true});
24=> "5/27/2078"
25```
26
27This will return a date string of the format MM/DD/YYYY.
28
29Now of course MM/DD/YYYY is the "American" date method, but it's the default
30because there isn't much support for internationalization here yet. Further,
31it's the format used by [Facebook][FB] and other services for birthdays and
32other non-Date object dates.
33
34However, we support returning dates in DD/MM/YYYY format as well when requesting
35a date by a string and passing `american: false`.
36
37```js
38chance.date({string: true, american: false});
39=> "13/2/2017"
40```
41
42If 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
44core competency to offer dates in a myriad of formats.
45
46Can optionally specify defaults for any of day, month, or year.
47
48```js
49chance.date({year: 1983});
50=> Wed May 04 1983 00:00:00 GMT-0400 (EDT)
51
52chance.date({month: 0});
53=> Tue Jan 18 2084 00:00:00 GMT-0500 (EST)
54
55chance.date({day: 21});
56=> Sun Oct 21 2103 00:00:00 GMT-0400 (EDT)
57```
58
59A random date is generated, but the default you specify is kept constant.
60
61Note, 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
63considered
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