npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

date-spoofer

v1.0.3

Published

Spoofing dates for browser & node

Downloads

340

Readme

DateSpoofer

DateSpoofer is a library for mocking the Date object and the current time. What it does is it lets you add an abstraction for the date which received either from new Date(...) or from Date.now().

Then, you can get a tweeked version of the original Date. Finally, you can call .apply() to set it as the new global Date. (And .restore() to reset it)

Installation

Start with

npm i date-spoofer

and

const DateSpoofer = require('date-spoofer')
// or
import {create, restore} from 'date-spoofer'

Usage

The usage is pretty simple, just call create() to create a new date class, and then apply it so it'll be global:

const DateSpoofer = require('date-spoofer')

DateSpoofer.create().apply()
// Date !== the original Date

DateSpoofer.restore()
// Date === the original Date

The magic happens in create(), it receives a callback that manipulates the time and will be used for every created Date:

// Time will always be Jan 1st, 1970
DateSpoofer.create((time) => {
  return 0
}).apply()

// Time will always be one second ago
DateSpoofer.create((time) => {
  return time - 1000
}).apply()

// Time will always be one second into the future!
DateSpoofer.create((time) => {
  return time + 1000
}).apply()

// Time will always have no milliseconds
DateSpoofer.create((time) => {
  return time - (time % 1000)
}).apply()

DateSpoofer.restore() // always nice to clean up

It's important to notice that create() is returning a new Date class, which you don't actually have to apply as default:

const DateSpoofer = require('date-spoofer')

const AlternativeDate = DateSpoofer.create((time) => time - (1000 * 60 * 60 * 24 * 7)) // always last week

console.log(AlternativeDate === Date) // false
console.log(AlternativeDate.now() === Date.now()) // false
console.log((new AlternativeDate().getTime()) === (new Date().getTime())) // false

With this instance, of course, we can apply it as default whenever we want

AlternativeDate.apply()

console.log(AlternativeDate === Date) // true
console.log(AlternativeDate.now() === Date.now()) // true
console.log((new AlternativeDate().getTime()) === (new Date().getTime())) // true

// will work only if it's the active Date
AlternativeDate.restore()

// in any case, a global restore is always an option
DateSpoofer.restore()

Specific times

It's also important to notice that this library will abstract only the current date! It means only new Date() with no argument and Date.now() will produce the modified date. Calling new Date(timestamp) will create this very timestamp.

DateSpoofer.create((time) => time - 1000).apply()

Date.now() // a second ago
new Date() // a second ago
new Date(0) // Jan 1st, 1970

However, if you need dynamic creation for all of the dates, create() accepts a second options argument exactly for that. But keep in mind though that every creation from another date is changing what you're getting, which might be an issue.

DateSpoofer.create((time) => time - 1000, {modifyEveryCreation: true}).apply()

Date.now() // a second ago
new Date() // a second ago
new Date(0) // -1000
new Date(Date.now()) // two seconds ago
new Date((new Date((new Date()).getTime())).getTime()) // three seconds ago

Test

100% code coverage

npm test