npm

@ttzschach/period
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

@ttzschach/period

Date utility library to help define periods as a pair of two Date objects consisting of a "from" & "to" date.
Allows for historic, prospective and even mixed Period definitions, thus creating periods that range from the past into the future.

import { getPeriodDates, Period, PeriodDates, TimeDirection, TimeUnit } from "@ttzschach/period";


const yesterdayToTomorrow: Period = {
    from: {
        timeUnit: TimeUnit.DAY,
        timeUnitCount: 1,
        timeDirection: TimeDirection.PAST,
    },
    to: {
        timeUnit: TimeUnit.DAY,
        timeUnitCount: 1,
        timeDirection: TimeDirection.FUTURE,
    }
};

const resolvedPeriodDates: PeriodDates | undefined = getPeriodDates(yesterdayToNow);
// ^
// interface PeriodDates {
//     from: Date;
//     to: Date;
// }

if (resolvedPeriodDates) {
    console.log(resolvedPeriodDates);
    // assuming getPeriodDates was executed roughly at 2024-03-30-12:00:00 it would output the following:
    // Object { from: 2024-03-29-12:00:00, to: 2024-03-31-12:00:00 }
}

As most properties are optional the defaults are defined to resolve yesterday for the "from" date and today for the "to" date.
You may find the defaults inside ./src/model.ts.

Installation

npm install @ttzschach/period

Uninstallation

npm uninstall @ttzschach/period

License

MIT

Package Sidebar

Install

npm i @ttzschach/period

Weekly Downloads

3

Version

1.0.2

License

MIT

Unpacked Size

23.6 kB

Total Files

14

Last publish

Collaborators

  • ttzschach