An Extended Date Time Format (EDTF) / ISO 8601-2 parser and toolkit for
date/time hackers and enthusiasts.
Compatibility
EDTF / ISO 8601-2
EDTF.js fully implements EDTF
levels 0, 1, and 2 as specified by ISO 8601-2 with the following
exceptions:
Seasons in intervals are supported at the experimental/non-standard
level 3. To enable season intervals, enable seasonIntervals in
defaults or when passing constraints to the parse function.
Uncertain or approximate seasons as well as seasons with unspecifed
years are supported at the experimental/non-standard level 3.
To enable this feature, set seasonUncertainty in defaults
or when passing constraints to the parse function.
To enable all experimental/non-standard features
you can also set the level to 3 in defaults or when passing
parser constraints.
ESM
Since version 4.0 EDTF.js uses Nodes.js native ESM module implementation.
Since version 4.1 EDTF.js is published as a dual-package.
Installation
$ npm install edtf
Manual
EDTF.js exports a top-level function which takes either a string
(with optional parser constraints), a parse result, a regular or any
of the extended date objects and returns a new, extended date object
as appropriate.
import edtf, { Date, Interval } from 'edtf'edtf('2016-XX') #-> returns a EDTF Dateedtf(new Date()) #-> returns a EDTF Dateedtf('2016-04~/2016-05') #-> returns a EDTF Interval
For a list of all types supported by EDTF.js see:
import * as types from 'edtf/types'Object.keys(types)#-> ['Date', 'Year', 'Decade', 'Century', 'Season', 'Interval', 'List', 'Set']
Each type provides at least the following properties: the date's
corresponding EDTF string, its minimum and maximum numeric value,
its type, as well as its date part values.
edtf('2016?').edtf #-> '2016?'edtf('2016-02').min #-> 1454284800000, i.e. 2016-02-01T00:00:00Zedtf('2016-02').max #-> 1456790399999, i.e. 2016-02-29T23:59:59Zedtf('2016-02-2X').min #-> 1455926400000, i.e. 2016-02-20T00:00:00Zedtf('2016-02-2X').max #-> 1456790399999, i.e. 2016-02-29T23:59:59Zedtf('[..2016,2017]').min #-> -Infinityedtf('[..2016,2017]').max #-> 1514764799999, i.e. 2017-12-31T23:59:59Zedtf('2016-34').min #-> 1459468800000, i.e. 2016-04-01T00:00:00Zedtf('2016-34').max #-> 1467331199999, i.e. 2016-06-30T23:59:59Zedtf('2016?-02').values #-> [2016, 1]edtf('2016-05').values #-> [2016, 4]
A date's min value is also used as its primitive value for numeric
coercion. Because this is the case for all EDTF.js classes, comparison
semantics tend to align well with common-sense expectations -- but be
careful, as always, when type coercion is at play.
Unspecified, uncertain, and approximate dates
EDTF.js keeps track of qualified dates using bitmasks. If you are interested
in binary yes or no, you can always convert a bitmask's value to boolean. For
more fine-grained information, the Bitmask class provides a convenient
interface for accessing these states:
Instead of year, month, and day, you can also query a string-based
representation of the bitmask:
edtf('201X-XX').unspecified.test('XXYYMMDD') #-> 0 / no edtf('201X-XX').unspecified.test('YYYXMMDD') #-> 8 / yes
When printing qualified dates, EDTF.js will try to find an optimal
rendtition of qualification symbols. For that reason, you can use EDTF.js
to normalize EDTF strings:
August 24, 2016 is covered by the interval '2016-06/2016-09' but is not
included in it, because the interval has month precision and can be
iterated as:
Careful, if your date has no precision, next will return the next
second!
The generator *between() returns all the dates, by precision,
between two dates; similarly, *until() returns all dates in between
and includes the first of the two dates; to generate the full range
inluding both dates, use *through().
The EDTF.js classes Date, Interval, List, and Set (lists model
EDTF's 'multiple dates', while sets model 'one of a set') are iterable.
Dates are iterable.
To use EDTF.js' date parser directly, call parse() with an input
string and optional parser constraints. The parser will always return
plain JavaScript objects which you can pass to edtf() for conversion
to extended date object, or to your own post-processing.
As you can see, the parser output includes the compatibility level of
the date parsed; the values array contains the individual date parts
in a format compatible with JavaScript's Date semantics (months are
a zero-based index).
Unspecified, uncertain, or approximate dates are returned as a numeric
value representing a bitmask; refer to the Bitmask class for details.
Note that unspecified date values will always return the least possible value,
e.g., '2000' for '20XX'. Note, also, that EDTF.js will not parse impossible
unspecified dates. For instance, none of the following examples can be
valid dates:
parse('2016-02-3X') #-> A day in February cannot start with a 3parse('2016-2X-XX') #-> There are only 12 monthsparse('2016-XX-32') #-> No month has 32 days
Intervals, Sets, and Lists will contain their parsed constituent dates in
the values array: