Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ranged-date

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ranged-date

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.

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-81.82%
Maintainers
1
Weekly downloads
 
Created
Source

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 command will test the package for errors.

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

Keywords

FAQs

Package last updated on 11 Apr 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc