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

datetoken

v1.1.4

Published

Parse relative datetime tokens into date objects

Downloads

17,226

Readme

Datetoken CircleCI Dependabot Status

Installing

npm i datetoken

Examples

Most probably you will be dealing with simple presets such as yesterday or the last 24 hours.

>>> const datetoken = require('datetoken');
>>> console.log(new Date())
2018-10-18 14:08:47
>>> datetoken('now-d/d')  # Start of yesterday
2018-10-17 00:00:00
>>> datetoken('now-d@d')  # End of yesterday
2018-10-17 23:59:59

Motivation

This package aims to solve a set of needs present in applications where dates need to be represented in a relative fashion, like background periodic tasks, datetime range pickers... in a compact and stringified format. This enables the programmer to persist these tokens during the lifetime of a process or even longer, since calculations are performed in the moment of evaluation. Theses tokens are also useful when caching URLs as replacement of timestamps, which would break caching given their mutability nature.

Some common examples of relative tokens:

| | From | To | | ------------------------------ | -------------- | ------------- | | Today | now/d | now | | Yesterday | now-d/d | now-d@d | | Last 24 hours | now-24h | now | | Last business week | now-w/bw | now-w@bw | | This business week | now/bw | now@bw | | Last month | now-1M/M | now-1M@M | | Next week | now+w/w | now+w@w | | Custom range | now+w-2d/h | now+2M-10h | | Last month first business week | now-M/M+w/bw | now-M/+w@bw | | This year | now/Y | now@Y | | This quarter | now/Q | now@Q | | First quarter (Q1) | now/Q1 | now@Q1 | | Second quarter (Q2) | now/Q2 | now@Q2 | | Third quarter (Q3) | now/Q3 | now@Q3 | | Fourth quarter (Q4) | now/Q4 | now@Q4 |

As you may have noticed, token follow a pattern:

  • The word now. It means the point in the future timeline when tokens are parsed to their datetime form.

  • Optionally, modifiers to add and/or subtract the future value of now can be used. Unsurprisingly, additions are set via +, while - mean subtractions. These modifiers can be chained as many times as needed. E.g: now-1M+3d+2h. Along with the arithmetical sign and the amount, the unit of time the amount refers to must be specified. Currently, the supported units are:

    • s seconds
    • m minutes
    • h hours
    • d days
    • w weeks
    • M months
    • Y years
    • Q quarters
  • Optionally, there exist two extra modifiers to snap dates to the start or the end of any given snapshot unit. Those are:

    • / Snap the date to the start of the snapshot unit.
    • @ Snap the date to the end of the snapshot unit.

    Snapshot units are the same as arithmetical modifiers, plus bw, meaning business week. With this, we achieve a simple way to define canonical relative date ranges, such as Today or Last month. As an example of the later:

    • String representation: now-1M/M, now-1M@M
    • Being today 15 Jan 2018, the result range should be: 2018-01-01 00:00:00 / 2018-01-31 23:59:59

Compatibilty

  • For node>=8.10 use datetoken==1.x.x
  • Otherwise use datetoken==0.x.x

Issues

  • Business week snapshots might not be reliable in timezones where weeks start in days other than Monday