`dates-range-parser` is a JavaScript library designed to parse and interpret human-readable date and time expressions, converting them into structured date ranges. It is currently at version 1.1.0 and appears to have an active development status, with its latest minor release introducing target timezone calculations. The library differentiates itself by supporting a wide array of formats, including relative expressions like 'yesterday', 'next week', and 'now -> 7days', as well as specific date/time strings and duration-based ranges such as '3days'. A key feature is its flexible timezone handling through `UTC` and `TZ` properties, allowing calculations in local time, UTC, or a specified target timezone. It also permits overriding the definition of 'now' for consistent testing or specific scenario handling. The output format consistently provides `start` and `end` properties, representing seconds since epoch, or `null` for unconstrained ranges. This output is often directly consumable by databases or search services.
npm install dates-range-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import and use the `DatesRangeParser` to interpret various date expressions, including relative and duration-based ranges, considering UTC and specific time zones. It also highlights how to interpret the output for JavaScript Date objects.
Multiply the `start` and `end` values by 1000 before creating a JavaScript `Date` object: `new Date(range.start * 1000)`.
Set `DatesRangeParser.UTC = true;` at the beginning of your application or before a block of UTC-dependent parsing. Remember to reset it or manage its state carefully if your application requires mixed local and UTC calculations.
Set `DatesRangeParser.TZ = 'Your/Timezone';` (e.g., 'Asia/Karachi') to perform relative calculations in that timezone. Ensure this is managed in contexts where multiple timezones might be relevant.
Override `DatesRangeParser.now` only when necessary (e.g., for testing or specific batch processing) and consider resetting it to `undefined` or `new Date()` afterwards to revert to the system's current time.
Always check the return value of `DatesRangeParser.parse()` for `null` before attempting to access its `start` or `end` properties: `const result = DatesRangeParser.parse(input); if (result) { /* use result */ } else { /* handle invalid input */ }`.For ESM, use `import DatesRangeParser from 'dates-range-parser';`. For CommonJS, use `const DatesRangeParser = require('dates-range-parser');`. Ensure you are calling `parse` as a static method on the imported object.Verify the `DatesRangeParser.UTC` and `DatesRangeParser.TZ` settings. Remember that `start` and `end` are seconds since epoch; multiply by 1000 for JavaScript Date objects: `new Date(range.start * 1000)`. If `TZ` is set, the `from`/`to` values in `value` object are milliseconds but represent the start/end of the day *in the target timezone*, which might convert to a different UTC day.
Consult the 'Quick Syntax Guide' and 'Syntax in More Detail' in the README for exact supported open-ended formats. For 'open to the future', `end` will be `null`. For 'open to the past', `start` will be `null`. Ensure your input string precisely matches documented patterns.
No dependency data recorded yet.