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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@alanszp/relative-date

v20.3.0

Published

Relative date utils.

Readme

Relative Date

Date Expressions

A date expression encapsulates the logic for absolute and relative date. They may contain an optional modifier

Modifiers

Modifiers allow you to adjust a date to the beginning or end of a specific time period. They are appended to date expressions using a colon (:) separator.

Syntax

<date_expression>:<modifier>

Where:

  • <date_expression> is any valid date (absolute, relative, or "now")
  • <modifier> follows the pattern [se]o[unit]
    • s = start of
    • e = end of
    • o = literal "o"
    • [unit] = time unit (see below)

Available Time Units

| Unit | Description | Example | | ---- | ------------------- | ------------------------- | | y | Year | eoy = end of year | | s | Semester (6 months) | sos = start of semester | | t | Third (4 months) | sot = start of third | | q | Quarter (3 months) | soq = start of quarter | | b | Bimester (2 months) | eob = end of bimester | | m | Month | eom = end of month | | w | Week | som = start of month |

Examples

Basic Usage:

// Start of month
DateExpression.from("2023-06-15:som"); // Returns 2023-06-01
DateExpression.from("now:som"); // Returns start of current month

// End of month
DateExpression.from("2023-06-15:eom"); // Returns 2023-06-30
DateExpression.from("now:eom"); // Returns end of current month

// Start of year
DateExpression.from("2023-06-15:soy"); // Returns 2023-01-01
DateExpression.from("now:soy"); // Returns start of current year

// End of year
DateExpression.from("2023-06-15:eoy"); // Returns 2023-12-31
DateExpression.from("now:eoy"); // Returns end of current year

With Relative Dates:

// Start of month for a date 5 days from now
DateExpression.from("+5d:som"); // Returns start of month for date 5 days from now

// End of quarter for a date 2 months ago
DateExpression.from("-2m:eoq"); // Returns end of quarter for date 2 months ago

// Start of year for yesterday
DateExpression.from("-1d:soy"); // Returns start of year for yesterday

Advanced Time Periods:

// Semester (6-month periods: Jan-Jun, Jul-Dec)
DateExpression.from("2023-04-15:sos"); // Returns 2023-01-01 (start of semester)
DateExpression.from("2023-08-15:eos"); // Returns 2023-12-31 (end of semester)

// Bimester (2-month periods: Jan-Feb, Mar-Apr, etc.)
DateExpression.from("2023-03-15:sob"); // Returns 2023-03-01 (start of bimester)
DateExpression.from("2023-05-15:eob"); // Returns 2023-04-30 (end of bimester)

// Third (4-month periods: Jan-Apr, May-Aug, Sep-Dec)
DateExpression.from("2023-06-15:sot"); // Returns 2023-05-01 (start of third)
DateExpression.from("2023-07-15:eot"); // Returns 2023-08-31 (end of third)

Week Boundaries:

// Start of week (Sunday by default)
DateExpression.from("2023-06-15:sow"); // Returns start of week containing June 15

// End of week (Saturday by default)
DateExpression.from("2023-06-15:eow"); // Returns end of week containing June 15

Quarter Boundaries:

// Quarters: Q1 (Jan-Mar), Q2 (Apr-Jun), Q3 (Jul-Sep), Q4 (Oct-Dec)
DateExpression.from("2023-05-15:soq"); // Returns 2023-04-01 (start of Q2)
DateExpression.from("2023-08-15:eoq"); // Returns 2023-09-30 (end of Q3)

Class Architecture

classDiagram
    class DateExpression {
        -raw: string
        -rawDate: string
        -modifier: Modifier | null
        -dateParser: DateParser
        +isNow: boolean
        +isRelativeDate(): boolean
        +isAbsoluteDate(): boolean
        +hasModifier(): boolean
        +toDate(): Date
        +toAbsoluteString(): string
        +modify(modifier: Modifier): DateExpression
        +decomposeRelativeDate(): RelativeDateDecomposition | null
        +toJSON(): object
        +static isValid(expression: string): boolean
        +static from(expression: string): DateExpression
        +static now(): DateExpression
        +static yesterday(): DateExpression
    }

    class Modifier {
        -extreme: PeriodModifierExtreme
        -unit: PeriodModifierTimeUnit

        +static startOf(unit: PeriodModifierTimeUnit): Modifier
        +static endOf(unit: PeriodModifierTimeUnit): Modifier
        +static from(modifier: string): Modifier

        +apply(date: Date): Date
        +isStart(): boolean
        +isEnd(): boolean
        +toString(): string
        +toJSON(): object
    }

    class DateParser {
        <<interface>>
        +toJSDate(raw: string): Date
    }

    class AbsoluteDateParser {
        +toJSDate(raw: string): Date
        +static isAbsoluteDate(raw: string): boolean
    }

    class RelativeDateParser {
        +toJSDate(raw: string): Date
        +decompose(raw: string): RelativeDateDecomposition
        +static isRelativeDate(raw: string): boolean
    }

    %% Relationships
    DateExpression --> Modifier : has optional
    DateExpression --> DateParser : uses

    DateParser <|.. AbsoluteDateParser : implements
    DateParser <|.. RelativeDateParser : implements