UNPKG

1.71 kBMarkdownView Raw
1MockDate
2========
3
4A JavaScript Mock Date object that can be used to change when "now" is.
5
6[![Build Status](https://travis-ci.org/boblauer/MockDate.png)](https://travis-ci.org/boblauer/MockDate)
7
8## Installation ##
9`npm install mockdate --save-dev`
10
11## Environment Support ##
12MockDate has been tested in Node, IE9+, Chrome, Firefox, and Opera.
13
14## Usage ##
15```javascript
16import MockDate from 'mockdate'
17```
18
19## API ##
20```javascript;
21MockDate.set(date)
22```
23
24#### __date__
25
26__date__: __`Object`__
27
28The `Date` to be returned when no parameters are passed to `new Date()`. Supports any object that has a `.valueOf` method that returns a value that can be passed to `new Date()`.
29
30__date__: __`String`__
31
32The string representation of the date which is passed to the `new Date()` constructor. This creates the `Date` to be returned when no parameters are passed to `new Date()`.
33
34__date__: __`Number`__
35
36The millisecond representation of the `Date` to be returned when no parameters are passed to `new Date()`.
37
38```javascript
39MockDate.reset();
40```
41
42Will restore the original `Date` object back to the native implementation.
43
44## Example ##
45```javascript
46MockDate.set('2000-11-22');
47
48new Date().toString() // "Tue Nov 21 2000 18:00:00 GMT-0600 (CST)"
49
50MockDate.set('1/30/2000');
51
52new Date().toString() // "Sun Jan 30 2000 00:00:00 GMT-0600 (CST)"
53
54MockDate.set(new Date('2/20/2000'));
55
56new Date().toString() // "Sun Feb 20 2000 00:00:00 GMT-0600 (CST)"
57
58MockDate.set(moment('3/30/2000').toDate()); // using momentjs
59
60new Date().toString() // "Thu Mar 30 2000 00:00:00 GMT-0600 (CST)"
61
62MockDate.reset();
63
64new Date().toString() // "Mon Mar 17 2014 18:08:44 GMT-0500 (CDT)"
65```
66
67## Test ##
68```javascript
69npm test
70```