1.16.2 • Published 1 year ago

@tuplo/datecalc v1.16.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

datecalc

Perform date calculations given a simple string pattern.

Install

$ npm install @tuplo/datecalc

# or with yarn
$ yarn add @tuplo/datecalc

Usage

import datecalc from "@tuplo/datecalc";

Performs calculations assuming now by default

// assuming New Year's Day 2020, 14:00
// in two days, same time
datecalc("2D"); // → new Date('2020-01-03T14:00:00')

// two weeks ago
datecalc("-2W"); // → new Date('2019-12-18T14:00:00')

// in 15 minutes
datecalc("15m"); // → new Date('2020-01-01T14:15:00')

// tomorrow, two hours less than now
datecalc("1D -2h"); // → new Date('2020-01-02T12:00:00')

Performs successive calculations

// 58 years and 2 months from now, less 4 hours
datecalc("58Y 2M -4h"); // → new Date('2078-03-01T10:00:00')

Calculates from a given date

const birthday = new Date("1961-07-04T11:00:00");
// 58 years from birthday
datecaclc("58Y", birthday); // → new Date('2019-07-04T11:00:00')

Accepted patterns

PatternPeriod
DDay
WWeek
MMonth
YYear
hHour
mMinute
sSecond

API

datecalc(format: string, fromDate?: Date): Date

pattern: string

The pattern declaring what calculations to perform.

Format: /(0-9-+DWMYhms)/g

fromDate: Date

The starting moment from when to start calculating.

Default: new Date(Date.now())

Return value: Date

Returns a new Date

Contribute

Contributions are always welcome!

1.16.2

1 year ago

1.16.1

1 year ago

1.16.0

2 years ago

1.15.0

2 years ago

1.14.0

3 years ago

1.13.0

3 years ago

1.12.0

3 years ago

1.9.0

3 years ago

1.8.0

3 years ago

1.11.0

3 years ago

1.10.0

3 years ago

1.7.0

3 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.3.0

4 years ago

1.0.0

4 years ago