@nrk/simple-date-parse
Simple natural language date parsing in javascript
Installation
npm i @nrk/simple-date-parse --save
import parse from '@nrk/simple-date-parse'
Usage
parse(
Date|Number|String,
[Number|Date]
)
parse(0|''|null)
parse('foobar')
parse('now')
parse('+ 1 second')
parse('- 2 minutes')
parse('+ 1 day')
parse('- 2 weeks')
parse('+ 1 month')
parse('- 2 years')
parse('00:00 - 1 year')
parse('yyyy-mm-01 + 1 year')
parse('mon + 1 days')
parse('friday')
parse('friday - 1 week')
parse('friday + 1 week')
parse('2018-01-dd + 1 week')
parse('yyyy-mm-01 + 1 month(s) - 1 day')
parse('yy00-01-01 - 100 year(s)')
parse('100-1-1')
parse('-100-1-1')
parse('y00', new Date(-081, 1, 1)).getFullYear()
parse('y0y', new Date(-081, 1, 1)).getFullYear()
parse('y0', new Date(-081, 1, 1)).getFullYear()
parse('y0yy', new Date(-081, 1, 1)).getFullYear()
parse('y0', new Date(-081, 1, 1)).getFullYear()
parse('y-0-0')
parse('y-4-90')
FAQ
Why did you make simple-date-parse
?
There is already a great variety of superb natural language date parsing libraries in javascript, but a great deal of them are quite forgiving and flexible in terms for formatting (for instance allowing both day before and month, understanding both "tomorrow" and "+ 1 day" etc.). Such libraries are wonderful when you are not in control of the input format, but they also comes with lots of kilobytes and needs for config for internationalisation.
simple-date-parse
is more simple and strict in terms of format (allowing only +/-
year|month|week|day|hour|minute|second
, mon|tue|wed|tur|sat|sun
and y-m-d
), making parsing blazing fast and super lightweight (1.27KB gzipped). Use simple-date-parse
generating interfaces like <button value="+ 1 day">Tomorrow</button>
or other situations with controlled text input.
When should I not use simple-date-parse
?
If you are doing date manipulation, we recommend a functional approaches provided by libraries such as Day.JS, Moment.js or date-fns. For more advanced natural language date parsing, checkout SugarJS, DateJS, Sherlock or Chrono.
Does simple-date-parse
handle timezones, leap year, summertime etc.?
TL;DR: yes. Despite popular belief, the native Date object in Javascript is quite intelligent. Under the hood simple-date-parse
converts operations to native function such as + 1 month === setMonth(date.getMonth() + 1)
. This avoid crazy calculations (such as year % 4 === leap year
or new Date().getTime() + 1000 * 60 * 60 24 * 10 === 10 days from now
) and lets native Date
to do the heaving lifting.
Local development
First clone @nrk/simple-date-parse
and install its dependencies:
git clone git@github.com:nrkno/simple-date-parse.git
cd simple-date-parse
npm install && npm test:watch
Building and committing
After having applied changes, remember to build before pushing the changes upstream.
git checkout -b feature/my-changes
npm run build
git commit -am "Add my changes"
git push origin feature/my-changes
NOTE! Please also make sure to keep commits small and clean (that the commit message actually refers to the updated files).
Stylistically, make sure the commit message is Capitalized and starts with a verb in the present tense (for example Add minification support
).