Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/boblauer/MockDate
A JavaScript Mock Date object that can be used to change when "now" is.
https://github.com/boblauer/MockDate
Last synced: 4 months ago
JSON representation
A JavaScript Mock Date object that can be used to change when "now" is.
- Host: GitHub
- URL: https://github.com/boblauer/MockDate
- Owner: boblauer
- License: mit
- Created: 2014-03-17T22:23:30.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2024-08-31T11:44:10.000Z (6 months ago)
- Last Synced: 2024-10-29T15:12:55.078Z (4 months ago)
- Language: JavaScript
- Size: 229 KB
- Stars: 664
- Watchers: 5
- Forks: 49
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
MockDate
========A JavaScript Mock Date object that can be used to change when "now" is.
## Installation ##
`npm install mockdate --save-dev`## Environment Support ##
MockDate has been tested in Node, IE9+, Chrome, Firefox, and Opera.## Usage ##
```javascript
import MockDate from 'mockdate'
```## API ##
```javascript;
MockDate.set(date)
```#### __date__
__date__: __`Object`__
The `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()`.
__date__: __`String`__
The 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()`.
__date__: __`Number`__
The millisecond representation of the `Date` to be returned when no parameters are passed to `new Date()`.
```javascript
MockDate.reset();
```Will restore the original `Date` object back to the native implementation.
## Example ##
```javascript
MockDate.set('2000-11-22');new Date().toString() // "Tue Nov 21 2000 18:00:00 GMT-0600 (CST)"
MockDate.set('1/30/2000');
new Date().toString() // "Sun Jan 30 2000 00:00:00 GMT-0600 (CST)"
MockDate.set(new Date('2/20/2000'));
new Date().toString() // "Sun Feb 20 2000 00:00:00 GMT-0600 (CST)"
MockDate.set(moment('3/30/2000').toDate()); // using momentjs
new Date().toString() // "Thu Mar 30 2000 00:00:00 GMT-0600 (CST)"
MockDate.reset();
new Date().toString() // "Mon Mar 17 2014 18:08:44 GMT-0500 (CDT)"
```## Test ##
```javascript
npm test
```