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
import MockDate from 'mockdate'
API
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()
.
MockDate.reset();
Will restore the original Date
object back to the native implementation.
Example
MockDate.set('2000-11-22');
new Date().toString()
MockDate.set('1/30/2000');
new Date().toString()
MockDate.set(new Date('2/20/2000'));
new Date().toString()
MockDate.set(moment('3/30/2000').toDate());
new Date().toString()
MockDate.reset();
new Date().toString()
Test
npm test