Socket
Book a DemoInstallSign in
Socket

mockdate

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mockdate

A JavaScript mock Date object that can be used to change when "now" is.

Source
npmnpm
Version
2.0.5
Version published
Weekly downloads
893K
9.55%
Maintainers
1
Weekly downloads
 
Created
Source

MockDate

A JavaScript Mock Date object that can be used to change when "now" is.

Build Status

Installation

npm install mockdate

Environment Support

MockDate has been tested in Node, IE9+, Chrome, Firefox, and Opera.

Usage

// CommonJS
var MockDate = require('mockdate');
// AMD
require(['MockDate'], function(MockDate) { ... });
// Script Tag
var MockDate = window.MockDate;

API

MockDate.set(date, [timezoneOffset])

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().

__timezoneOffset

timezoneOffset: Number

The value that should be returned by new Date().getTimezoneOffset()

MockDate.reset();

Will restore the original Date object back to the native implementation.

Example

MockDate.set('2000-11-22');

new Date().toString() // "Wed Nov 22 2000 00: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('1/30/2000', 120);

new Date().getTimezoneOffset() // 120

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')); // 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)"
new Date().getTimezoneOffset() // 360

Test

npm test

Keywords

date

FAQs

Package last updated on 09 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts