Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including ranged-date with all npm packages installed. Try it out:

var rangedDate = require("ranged-date")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

ranged-date v1.1.3

Converts a given string, number, or Date object to the number of milliseconds since the Unix epoch, provided that it can be recognized as millisecond, second, or microsecond time within a specified range from the current date.

ranged-date

NPM

Converts a given string, number, or Date object to the number of milliseconds since the Unix epoch, provided that it can be recognized as millisecond, second, or microsecond time within a specified range from the current date.

Milliseconds, seconds, or microseconds may be excluded as possibilities for range matching.

Useful for:

  • Parsing timestampted data from external APIs.
  • Integrating collections containing mixed timestamp formats.

Warning:

  • Although it is unlikely for a given number to fall within the default range (one year), it is advisable to both restrict the range and use exclusions wherever possible, in order to avoid incorrectly classifying and converting values that were not intended to be timestamps.

Getting Started

Prerequisites

Node.JS version 6.0.0 or above.

Installing

npm i ranged-date

Testing

The following commands will test the package for errors.

cd /path/to/node_modules/ranged-date
npm test

Deployment

const rangedDate = require('ranged-date')

Usage

const sec    = ~~(Date.now() / 1000) // Test unixtime
const ms     = sec * 1000            // Test millisecond time
const us     = ms * 1000             // Test microsecond time
const past   = sec - 31536000        // Unixtime one year prior
const future = sec + 31536000        // Unixtime one year after

console.log(rangedDate(sec))                        // converted time in ms
console.log(rangedDate(ms))                         // time in ms
console.log(rangedDate(us))                         // converted time in ms
console.log(rangedDate(String(sec)))                // converted time in ms
console.log(rangedDate(past))                       // false
console.log(rangedDate(past, 2))                    // converted time in ms
console.log(rangedDate(future))                     // false
console.log(rangedDate(future, null, 2))            // converted time in ms
console.log(rangedDate(sec, 0.5, 0.5, { s: true })) // false
console.log(rangedDate(ms, 0.5, 0.5, { ms: true })) // false
console.log(rangedDate(us, 0.5, 0.5, { us: true })) // false
console.log(
  rangedDate(new Date(ms), 0.5, 0.5, { ms: true })
)                                                   // converted time in ms

Documentation

rangedDate ⇒ number | boolean

ParamTypeAttributesDefaultDescription
dataDate | number | stringData to attempt to recognize as valid date.
backnumber<optional>0.5Years before current date as lower bound.
fwdnumber<optional>0.5Years after current date as upper bound.
excluderangedDate~exclude<optional>{}Specifies range exclusions, if any.

Returns: number | boolean - Converted ms or false if outside range.

rangedDate~exclude

Settings for exclusion of milliseconds, seconds, or microseconds as possibilities for range matching.

Properties

NameTypeAttributesDescription
msboolean<optional>Exclude milliseconds.
sboolean<optional>Exclude seconds.
usboolean<optional>Exclude microseconds.

See

Versioning

Versioning using SemVer. For available versions, see the tags on this repository.

Author

  • Justin Collier - jpcx

License

This project is licensed under the ISC License - see the LICENSE.md file for details

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free