Parse a wide range of date formats including human-input dates.
Supports Node, IE11+ and evergreen browsers.
npm install any-date-parser
OR
<script src="https://cdn.jsdelivr.net/npm/any-date-parser@1.5.4/dist/browser-bundle.js"></script>
Intl
global objectThere are three ways to use any-date-parser:
1.) Use a new function directly on Date
:
Date.fromString(string, locale)
- Parses a string and returns a Date
objectDate.fromAny(any, locale)
- Return a Date
object given a Date
, Number
or string to parseExample:
require('any-date-parser');
Date.fromString('2020-10-15');
// same as new Date(2020, 9, 15, 0, 0, 0, 0)
2.) Use the parser object:
parser.fromString(string, locale)
- Parses a string and returns a Date
object. It is the same function as in option 1.parser.fromAny(any, locale)
- Return a Date
object given a Date
,
Number
or string to parse. It is the same function as in option 1.Example:
const parser = require('any-date-parser');
parser.fromString('2020-10-15');
// same as new Date(2020, 9, 15, 0, 0, 0, 0)
3.) It also exports parser
with function parser.attempt(string, locale)
that
returns an object with one or more integer values for the following keys: year,
month, day, hour, minute, second, millisecond, offset. Example:
const parser = require('any-date-parser');
parser.attempt('15 Oct 2020 at 6pm');
// returns:
{
year: 2020,
month: 10,
day: 15,
hour: 18,
}
4.) There are npm packages that integrate any-date-parser directly into popular date libraries:
Summary:
Exhaustive list of date formats
any-date-parser supports any locale that your runtime's Intl
(ECMAScript
Internationalization API) supports. In browsers that usually means the operating
system language. In Node, that means the compiled language or the icu modules
included. For unit tests, this library uses the
full-icu npm package to make all locales
available. That package is heavy and is not included as a dependency.
any-date-parser has an addFormat()
function to add a custom parser.
First, parsers must have matcher
or template
.
matcher
: A RegExp to match a stringtemplate
: A string with template variables such as _YEAR_
_MONTH_
etc.
that will be converted to a regular expressionSecond, parsers must have units
or handler
.
units
: An array of unit strings to fit matches into (year, month, day, etc.)handler
: A function that takes matches and returns an object with keys year,
month, day etc.const parser,
{ Format } = require('any-date-parser');
parser.addFormat(
new Format({
matcher: /^(\d+) days? into month (\d+) in year (\d{4})$/,
units: ['day', 'month', 'year'],
})
);
Keep in mind that \d
does not support other numbering system such as Chinese
or Bengali. To support those you can use the template
option given in
example 3 and
example 4.
const parser,
{ Format } = require('any-date-parser');
parser.addFormat(
new Format({
matcher: /^Q([1-4]) (\d{4})$/,
handler: function ([, quarter, year]) {
const monthByQuarter = { 1: 1, 2: 4, 3: 7, 4: 10 };
const month = monthByQuarter[quarter];
return { year, month };
},
})
);
const parser,
{ Format } = require('any-date-parser');
parser.addFormat(
new Format({
template: 'The (_DAY_)(?:_ORDINAL_) day of (_MONTH_), (_YEAR_)',
units: ['day', 'month', 'year'],
})
);
const parser,
{ Format } = require('any-date-parser');
parser.addFormat(
new Format({
template: '^Q([1-4]) (_YEAR_)$',
handler: function ([, quarter, year]) {
const monthByQuarter = { 1: 1, 2: 4, 3: 7, 4: 10 };
const month = monthByQuarter[quarter];
return { year, month };
},
})
);
To remove support for a certain format, use removeFormat()
const parser = require('any-date-parser');
const dayMonth = require('any-date-parser/src/formats/dayMonth/dayMonth.js');
parser.removeFormat(dayMonth);
To create a new parser with a limited list of formats or your own custom
formats, use new Parser
const { Parser } = require('any-date-parser');
const time24Hours = require('any-date-parser/src/formats/time24Hours/time24Hours.js');
const yearMonthDay = require('any-date-parser/src/formats/yearMonthDay/yearMonthDay.js');
const ago = require('any-date-parser/src/formats/ago/ago.js');
const parser = new Parser();
parser.addFormats([time24Hours, yearMonthDay, ago]);
You can convert your custom parser to a function. For example:
const { Parser } = require('any-date-parser');
const parser = new Parser();
// ....
parser.addFormats(/*...*/);
// Pass locale if you want to override the detected default
Date.fromString = parser.exportAsFunction();
Date.fromAny = parser.exportAsFunctionAny();
any-date-parser
has 100% code coverage.
npm test
npm run coverage
Contributions are welcome. Please open a GitHub ticket for bugs or feature requests. Please make a pull request for any fixes or new code you'd like to be incorporated.
24 hour time (any date format followed by a 24-hour time expression)
12 hour time (any date format followed by a 12-hour time expression)
year month day
day monthname year
monthname day year
month day year
day month year
relative time
monthname day
day monthname
month day
day month
unix timestamp
@1602604901
Microsoft JSON date string
/Date(1601677889008-0700)/
/Date(1601677889008)/
chinese
2020年09月26日
2020年9月26日
2020 年 9 月 26 日
2017年08月31日