time-range-parser
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

DateRangeParser

DateRangeParser is a TypeScript library for parsing and handling date ranges. It supports various date formats and relative time expressions, making it suitable for a wide range of date processing scenarios.

Features

  • Supports keywords such as now, today, thisweek, thismonth, thisyear, etc.
  • Supports relative time expressions like 6h, -6h, 2d, -3d, etc.
  • Handles complex date range operations using operators like -> and <>.

Installation

To install DateRangeParser, use npm:

npm install daterangeparser

Usage

Importing the Library

import DateRangeParser from 'daterangeparser';

Parsing Date Inputs

const result1 = DateRangeParser.parseDateInput('now');
console.log(result1); // { start: [current timestamp], end: [current timestamp] }

const result2 = DateRangeParser.parseDateInput('today');
console.log(result2); // { start: [start of today], end: [end of today] }

const result3 = DateRangeParser.parseDateInput('now -> 6h');
console.log(result3); // { start: [current timestamp], end: [current timestamp + 6 hours] }

const result4 = DateRangeParser.parseDateInput('now -> -6h');
console.log(result4); // { start: [current timestamp - 6 hours], end: [current timestamp] }

Handling Errors

const result = DateRangeParser.parseDateInput('invalid input');
if ('error' in result) {
    console.error(result.error); // Outputs: "Unknown keyword: invalidinput"
}

Readme

Keywords

none

Package Sidebar

Install

npm i time-range-parser

Weekly Downloads

47

Version

0.1.4

License

none

Unpacked Size

35.7 kB

Total Files

7

Last publish

Collaborators

  • ahdg