0.5.1 • Published 5 years ago

period-edge v0.5.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Period Edge

This package aims to help calculate the start & end timestamp of custom period, like 4 weeks. This is a sub-project of relative-time-expression.

Install

npm i period-edge

Usage

import { startOf, endOf } from 'period-edge';

// Suppose we are in Shanghai(+8h), and it's 2019-06-14 18:00:00.
expect(startOf(new Date(), '12h')).toEqual(new Date('2019-06-14T12:00:00+08:00'));
expect(endOf(new Date(), '12h')).toEqual(new Date('2019-06-14T23:59:59+08:00'));

Api

function startOf(date: Date, period: string): Date;
function endOf(date: Date, period: string): Date;

Period

Period constructed with integer and unit string, like 2w. Supported units: s, m, h, d, w.

Regular expression:

/(\d+)([smhdw])/

Special logic about week period

  1. Week period use monday as first day according to ISO 8601, if you want to use sunday, you could subtract one day to result.
  2. This lib will use 1970-01-05T00:00:00 in your timezone as first week to calculate multiple weeks period.