Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
any-date-parser
Advanced tools
The most comprehensive and accurate date parser for Node and browsers. It uses
Intl
to provide parsing support for all installed locales.
npm install any-date-parser
OR
<script src="https://cdn.jsdelivr.net/npm/any-date-parser@2.0.3/dist/browser-bundle.js"></script>
fromString
and fromAny
now return a MaybeValidDate
instance that is a
subclass of Date
. Previously, they returned Date | { invalid: string; }
.
MaybeValidDate
has an invalid
property if invalid, and an isValid()
function whether valid or not. If in v1 you simply checked for an invalid
property, v2 will behave the same.Intl
global objectThere are three ways to use any-date-parser
:
1.) Use the parser object: (Recommended)
parser.fromString(string, locale)
- Parses a string and returns a Date
object.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:
import parser from 'any-date-parser';
parser.fromString('2020-10-15');
// same as new Date(2020, 9, 15, 0, 0, 0, 0)
2.) parser
also has a 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. Note month is returned
as a normal 1-based integer, not the 0-based integer the Date()
constructor
uses.
Examples:
import parser from 'any-date-parser';
parser.attempt('15 Oct 2020 at 6pm');
/* returns:
{
year: 2020,
month: 10,
day: 15,
hour: 18,
}
*/
parser.attempt('Oct 15');
/* returns:
{
month: 10,
day: 15,
}
*/
parser.attempt('Hello world');
/* returns:
{ invalid: 'Unable to parse "Hello World"' }
*/
parser.attempt('');
/* returns:
{ invalid: 'Unable to parse "(empty string)"' }
*/
3.) 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:
import 'any-date-parser';
Date.fromString('2020-10-15');
// same as new Date(2020, 9, 15, 0, 0, 0, 0)
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.DateTimeFormat
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.
This means support for international formats such as:
es-MX
- viernes, 27 de septiembre de 2024, 10:39:50 a.m. GMT-6
bn-BD
- শুক্রবার, ২৭ সেপ্টেম্বর, ২০২৪ এ ১০:৩৬:১০ AM GMT -৬
el-GR
- Παρασκευή 27 Σεπτεμβρίου 2024 στις 10:38:16 π.μ. GMT-6
hi-IN
- शुक्रवार, 27 सितंबर 2024 को 10:39:13 am GMT-6 बजे
th-TH
- วันศุกร์ที่ 27 กันยายน พ.ศ. 2567 เวลา 10 นาฬิกา 40 นาที 28 วินาที GMT-6
ta-IN
- வெள்ளி, 27 செப்டம்பர், 2024 அன்று 10:43:05 AM GMT-6
hu-HU
- 2024. szeptember 27., péntek 10:44:41 GMT-6
Note: For locales that use the Buddhist year (such as th-TH
),
any-date-parser automatically subtracts 543 years to normalize it to the
Gregorian Calendar year.
Check out the list of all supported locales
-8640000000000000 through 8640000000000000
, which allows a range of
-271821-04-20T00:00:00.000Z through +275760-09-13T00:00:00.000Z
.islamic-umalqura
)ar
(Arabic) and he
(Hebrew)You can git checkout any-date-parser
and run its tests.
npm test
npm run coverage
npm test
will attempt to install full-icu
and luxon
globally if
not presentContributions 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 (for locales: ee-TG en-AS,CA,FM,GH,GU,KE,MH,MP,US,VI,WS jp-JP sm-AS,SM)
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日
fuzzy (some examples)
On Wed 8 March in the year 2020
In 1929, the stock market crashed on October 29
v2.0.3 on 2025-01-20
FAQs
Parse a wide range of date formats including human-input dates
The npm package any-date-parser receives a total of 7,910 weekly downloads. As such, any-date-parser popularity was classified as popular.
We found that any-date-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.