| 1 |
.. Note that we use raw HTML in the header section because centering images and paragraphs is not supported in Github (https://github.com/github/markup/issues/163) |
| 2 |
|
| 3 |
.. raw:: html |
| 4 |
|
| 5 |
<div align="center"> |
| 6 |
|
| 7 |
|
| 8 |
.. image:: artwork/dateparser-logo.png |
| 9 |
:alt: Dateparser |
| 10 |
:width: 500 |
| 11 |
:target: https://github.com/scrapinghub/dateparser |
| 12 |
|
| 13 |
|
| 14 |
**Python parser for human readable dates** |
| 15 |
|
| 16 |
|
| 17 |
.. raw:: html |
| 18 |
|
| 19 |
</div> |
| 20 |
|
| 21 |
|
| 22 |
.. docs-include-marker |
| 23 |
|
| 24 |
|
| 25 |
.. raw:: html |
| 26 |
|
| 27 |
<p align="center"> |
| 28 |
<a href="https://pypi.python.org/pypi/dateparser"> |
| 29 |
<img src="https://img.shields.io/pypi/dm/dateparser.svg" alt="PyPI - Downloads"> |
| 30 |
</a> |
| 31 |
<a href="https://pypi.python.org/pypi/dateparser"> |
| 32 |
<img src="https://img.shields.io/pypi/v/dateparser.svg" alt="PypI - Version"> |
| 33 |
</a> |
| 34 |
<a href="https://codecov.io/gh/scrapinghub/dateparser"> |
| 35 |
<img src="https://codecov.io/gh/scrapinghub/dateparser/branch/master/graph/badge.svg" alt="Code Coverage"> |
| 36 |
</a> |
| 37 |
<a href="https://github.com/scrapinghub/dateparser/actions"> |
| 38 |
<img src="https://github.com/scrapinghub/dateparser/workflows/Build/badge.svg" alt="Github - Build"> |
| 39 |
</a> |
| 40 |
<a href="https://dateparser.readthedocs.org/en/latest/?badge=latest"> |
| 41 |
<img src="https://readthedocs.org/projects/dateparser/badge/?version=latest" alt="Readthedocs - Docs"> |
| 42 |
</a> |
| 43 |
</p> |
| 44 |
|
| 45 |
<p align="center"> |
| 46 |
<a href="#key-features">Key Features</a> • |
| 47 |
<a href="#how-to-use">How To Use</a> • |
| 48 |
<a href="#installation">Installation</a> • |
| 49 |
<a href="#common-use-cases">Common use cases</a> • |
| 50 |
<a href="#you-may-also-like">You may also like...</a> • |
| 51 |
<a href="#license">License</a> |
| 52 |
</p> |
| 53 |
|
| 54 |
|
| 55 |
Key Features |
| 56 |
------------ |
| 57 |
|
| 58 |
- Support for almost every existing date format: absolute dates, |
| 59 |
relative dates (``"two weeks ago"`` or ``"tomorrow"``), timestamps, |
| 60 |
etc. |
| 61 |
- Support for more than `200 language |
| 62 |
locales <https://dateparser.readthedocs.io/en/latest/supported_locales.html>`__. |
| 63 |
- Language autodetection |
| 64 |
- Customizable behavior through |
| 65 |
`settings <https://dateparser.readthedocs.io/en/latest/settings.html>`__. |
| 66 |
- Support for `non-Gregorian calendar |
| 67 |
systems <#supported-calendars>`__. |
| 68 |
- Support for dates with timezones abbreviations or UTC offsets |
| 69 |
(``"August 14, 2015 EST"``, ``"21 July 2013 10:15 pm +0500"``...) |
| 70 |
- `Search dates <#search-for-dates-in-longer-chunks-of-text>`__ |
| 71 |
in longer texts. |
| 72 |
- Time span detection for expressions like "past month", "last week". |
| 73 |
|
| 74 |
Online demo |
| 75 |
----------- |
| 76 |
|
| 77 |
Do you want to try it out without installing any dependency? Now you can test |
| 78 |
it quickly by visiting `this online demo <https://gnyman.github.io/dateparser-demo-app/>`__! |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
How To Use |
| 83 |
---------- |
| 84 |
|
| 85 |
The most straightforward way to parse dates with **dateparser** is to |
| 86 |
use the ``dateparser.parse()`` function, that wraps around most of the |
| 87 |
functionality of the module. |
| 88 |
|
| 89 |
.. code:: python |
| 90 |
|
| 91 |
>>> import dateparser |
| 92 |
|
| 93 |
>>> dateparser.parse('Fri, 12 Dec 2014 10:55:50') |
| 94 |
datetime.datetime(2014, 12, 12, 10, 55, 50) |
| 95 |
|
| 96 |
>>> dateparser.parse('1991-05-17') |
| 97 |
datetime.datetime(1991, 5, 17, 0, 0) |
| 98 |
|
| 99 |
>>> dateparser.parse('In two months') # today is 1st Aug 2020 |
| 100 |
datetime.datetime(2020, 10, 1, 11, 12, 27, 764201) |
| 101 |
|
| 102 |
>>> dateparser.parse('1484823450') # timestamp |
| 103 |
datetime.datetime(2017, 1, 19, 10, 57, 30) |
| 104 |
|
| 105 |
>>> dateparser.parse('January 12, 2012 10:00 PM EST') |
| 106 |
datetime.datetime(2012, 1, 12, 22, 0, tzinfo=<StaticTzInfo 'EST'>) |
| 107 |
|
| 108 |
**dateparser** also works with strings in different languages: |
| 109 |
|
| 110 |
.. code:: python |
| 111 |
|
| 112 |
>>> dateparser.parse('Martes 21 de Octubre de 2014') # Spanish (Tuesday 21 October 2014) |
| 113 |
datetime.datetime(2014, 10, 21, 0, 0) |
| 114 |
|
| 115 |
>>> dateparser.parse('Le 11 Décembre 2014 à 09:00') # French (11 December 2014 at 09:00) |
| 116 |
datetime.datetime(2014, 12, 11, 9, 0) |
| 117 |
|
| 118 |
>>> dateparser.parse('13 января 2015 г. в 13:34') # Russian (13 January 2015 at 13:34) |
| 119 |
datetime.datetime(2015, 1, 13, 13, 34) |
| 120 |
|
| 121 |
>>> dateparser.parse('1 เดือนตุลาคม 2005, 1:00 AM') # Thai (1 October 2005, 1:00 AM) |
| 122 |
datetime.datetime(2005, 10, 1, 1, 0) |
| 123 |
|
| 124 |
>>> dateparser.parse('yaklaşık 23 saat önce') # Turkish (23 hours ago), current time: 12:46 |
| 125 |
datetime.datetime(2019, 9, 7, 13, 46) |
| 126 |
|
| 127 |
>>> dateparser.parse('2小时前') # Chinese (2 hours ago), current time: 22:30 |
| 128 |
datetime.datetime(2018, 5, 31, 20, 30) |
| 129 |
|
| 130 |
You can specify the language(s), if known, using the ``languages`` argument. |
| 131 |
In this case, given languages are used and language detection is skipped: |
| 132 |
|
| 133 |
.. code:: python |
| 134 |
|
| 135 |
>>> dateparser.parse('2015, Ago 15, 1:08 pm', languages=['pt', 'es']) |
| 136 |
datetime.datetime(2015, 8, 15, 13, 8) |
| 137 |
|
| 138 |
If you know the possible formats of the dates, you can use the |
| 139 |
``date_formats`` argument: |
| 140 |
|
| 141 |
.. code:: python |
| 142 |
|
| 143 |
>>> dateparser.parse('22 Décembre 2010', date_formats=['%d %B %Y']) |
| 144 |
datetime.datetime(2010, 12, 22, 0, 0) |
| 145 |
|
| 146 |
Relative Dates |
| 147 |
^^^^^^^^^^^^^^ |
| 148 |
|
| 149 |
.. code:: python |
| 150 |
|
| 151 |
>>> from dateparser import parse |
| 152 |
>>> parse('1 hour ago') |
| 153 |
datetime.datetime(2015, 5, 31, 23, 0) |
| 154 |
>>> parse('Il ya 2 heures') # French (2 hours ago) |
| 155 |
datetime.datetime(2015, 5, 31, 22, 0) |
| 156 |
>>> parse('1 anno 2 mesi') # Italian (1 year 2 months) |
| 157 |
datetime.datetime(2014, 4, 1, 0, 0) |
| 158 |
>>> parse('yaklaşık 23 saat önce') # Turkish (23 hours ago) |
| 159 |
datetime.datetime(2015, 5, 31, 1, 0) |
| 160 |
>>> parse('Hace una semana') # Spanish (a week ago) |
| 161 |
datetime.datetime(2015, 5, 25, 0, 0) |
| 162 |
>>> parse('2小时前') # Chinese (2 hours ago) |
| 163 |
datetime.datetime(2015, 5, 31, 22, 0) |
| 164 |
|
| 165 |
.. note:: Testing above code might return different values depending on your environment's current date and time. |
| 166 |
|
| 167 |
.. note:: For the ``Finnish`` language, please specify ``settings={'SKIP_TOKENS': []}`` to correctly parse relative dates. |
| 168 |
|
| 169 |
Date Order |
| 170 |
^^^^^^^^^^ |
| 171 |
|
| 172 |
.. code:: python |
| 173 |
|
| 174 |
>>> # parsing ambiguous date |
| 175 |
>>> parse('02-03-2016') # assumes english language, uses MDY date order |
| 176 |
datetime.datetime(2016, 2, 3, 0, 0) |
| 177 |
>>> parse('le 02-03-2016') # detects french, uses DMY date order |
| 178 |
datetime.datetime(2016, 3, 2, 0, 0) |
| 179 |
|
| 180 |
.. note:: Ordering is not locale-based — do not expect ``DMY`` order for UK/Australia English. |
| 181 |
You can specify date order explicitly: |
| 182 |
|
| 183 |
.. code:: python |
| 184 |
|
| 185 |
>>> parse('18-12-15 06:00', settings={'DATE_ORDER': 'DMY'}) |
| 186 |
datetime.datetime(2015, 12, 18, 6, 0) |
| 187 |
|
| 188 |
For more on date order, see the `settings documentation <https://dateparser.readthedocs.io/en/latest/settings.html>`__. |
| 189 |
|
| 190 |
Timezone and UTC Offset |
| 191 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 192 |
|
| 193 |
By default, `dateparser` returns a timezone-aware ``datetime`` if a timezone is |
| 194 |
present in the date string. Otherwise it returns a naive ``datetime`` object. |
| 195 |
|
| 196 |
.. code:: python |
| 197 |
|
| 198 |
>>> parse('January 12, 2012 10:00 PM EST') |
| 199 |
datetime.datetime(2012, 1, 12, 22, 0, tzinfo=<StaticTzInfo 'EST'>) |
| 200 |
|
| 201 |
>>> parse('January 12, 2012 10:00 PM -0500') |
| 202 |
datetime.datetime(2012, 1, 12, 22, 0, tzinfo=<StaticTzInfo 'UTC\-05:00'>) |
| 203 |
|
| 204 |
>>> parse('2 hours ago EST') |
| 205 |
datetime.datetime(2017, 3, 10, 15, 55, 39, 579667, tzinfo=<StaticTzInfo 'EST'>) |
| 206 |
|
| 207 |
If the date has no timezone name/abbreviation or offset, you can specify it |
| 208 |
using the ``TIMEZONE`` setting: |
| 209 |
|
| 210 |
.. code:: python |
| 211 |
|
| 212 |
>>> parse('January 12, 2012 10:00 PM', settings={'TIMEZONE': 'US/Eastern'}) |
| 213 |
datetime.datetime(2012, 1, 12, 22, 0) |
| 214 |
|
| 215 |
>>> parse('January 12, 2012 10:00 PM', settings={'TIMEZONE': 'US/Eastern', 'RETURN_AS_TIMEZONE_AWARE': True}) |
| 216 |
datetime.datetime(2012, 1, 12, 22, 0, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>) |
| 217 |
|
| 218 |
>>> parse('10:00 am', settings={'TIMEZONE': 'EST', 'TO_TIMEZONE': 'EDT'}) |
| 219 |
datetime.datetime(2016, 9, 25, 11, 0) |
| 220 |
|
| 221 |
>>> parse('10:00 am EST', settings={'TO_TIMEZONE': 'EDT'}) |
| 222 |
datetime.datetime(2017, 3, 12, 11, 0, tzinfo=<StaticTzInfo 'EDT'>) |
| 223 |
|
| 224 |
For more on timezones, see the `settings documentation <https://dateparser.readthedocs.io/en/latest/settings.html>`__. |
| 225 |
|
| 226 |
Incomplete Dates |
| 227 |
^^^^^^^^^^^^^^^^ |
| 228 |
|
| 229 |
.. code:: python |
| 230 |
|
| 231 |
>>> from dateparser import parse |
| 232 |
>>> parse('December 2015') # default behavior |
| 233 |
datetime.datetime(2015, 12, 16, 0, 0) |
| 234 |
>>> parse('December 2015', settings={'PREFER_DAY_OF_MONTH': 'last'}) |
| 235 |
datetime.datetime(2015, 12, 31, 0, 0) |
| 236 |
>>> parse('December 2015', settings={'PREFER_DAY_OF_MONTH': 'first'}) |
| 237 |
datetime.datetime(2015, 12, 1, 0, 0) |
| 238 |
|
| 239 |
>>> parse('March') |
| 240 |
datetime.datetime(2015, 3, 16, 0, 0) |
| 241 |
>>> parse('March', settings={'PREFER_DATES_FROM': 'future'}) |
| 242 |
datetime.datetime(2016, 3, 16, 0, 0) |
| 243 |
|
| 244 |
>>> import dateparser |
| 245 |
>>> dateparser.parse("2015") # default behavior |
| 246 |
datetime.datetime(2015, 3, 27, 0, 0) |
| 247 |
>>> dateparser.parse("2015", settings={"PREFER_MONTH_OF_YEAR": "last"}) |
| 248 |
datetime.datetime(2015, 12, 27, 0, 0) |
| 249 |
>>> dateparser.parse("2015", settings={"PREFER_MONTH_OF_YEAR": "current"}) |
| 250 |
datetime.datetime(2015, 3, 27, 0, 0) |
| 251 |
|
| 252 |
You can also ignore incomplete dates by setting the ``STRICT_PARSING`` flag: |
| 253 |
|
| 254 |
.. code:: python |
| 255 |
|
| 256 |
>>> parse('December 2015', settings={'STRICT_PARSING': True}) |
| 257 |
None |
| 258 |
|
| 259 |
For more on handling incomplete dates, see the `settings documentation <https://dateparser.readthedocs.io/en/latest/settings.html>`__. |
| 260 |
|
| 261 |
Search for Dates in Longer Chunks of Text |
| 262 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 263 |
|
| 264 |
.. warning:: Support for date searching is limited and needs improvement. |
| 265 |
Contributions are welcome — see `contributing <https://dateparser.readthedocs.io/en/latest/contributing.html>`__. |
| 266 |
|
| 267 |
You can extract dates from longer strings of text. Results are returned as a |
| 268 |
list of ``(substring, datetime)`` tuples: |
| 269 |
|
| 270 |
.. code:: python |
| 271 |
|
| 272 |
>>> from dateparser.search import search_dates |
| 273 |
>>> search_dates('Today is 25 of October 2017, so the 27th is in 2 days.') |
| 274 |
[('25 of October 2017', datetime.datetime(2017, 10, 25, 0, 0)), ('the 27th is in 2 days', datetime.datetime(2017, 10, 27, 0, 0))] |
| 275 |
|
| 276 |
Time Span Detection |
| 277 |
^^^^^^^^^^^^^^^^^^^ |
| 278 |
|
| 279 |
The ``search_dates`` function can also detect time spans such as |
| 280 |
"past month" or "last week". When ``RETURN_TIME_SPAN`` is enabled it |
| 281 |
returns start and end dates for the detected period: |
| 282 |
|
| 283 |
.. code:: python |
| 284 |
|
| 285 |
>>> search_dates("Messages from the past month", settings={'RETURN_TIME_SPAN': True}) |
| 286 |
[('past month (start)', datetime.datetime(2024, 11, 7, 0, 0)), |
| 287 |
('past month (end)', datetime.datetime(2024, 12, 7, 23, 59, 59, 999999))] |
| 288 |
|
| 289 |
Settings |
| 290 |
^^^^^^^^ |
| 291 |
|
| 292 |
You can control multiple behaviors by using the ``settings`` parameter: |
| 293 |
|
| 294 |
.. code:: python |
| 295 |
|
| 296 |
>>> dateparser.parse('2014-10-12', settings={'DATE_ORDER': 'YMD'}) |
| 297 |
datetime.datetime(2014, 10, 12, 0, 0) |
| 298 |
|
| 299 |
>>> dateparser.parse('2014-10-12', settings={'DATE_ORDER': 'YDM'}) |
| 300 |
datetime.datetime(2014, 12, 10, 0, 0) |
| 301 |
|
| 302 |
>>> dateparser.parse('1 year', settings={'PREFER_DATES_FROM': 'future'}) # Today is 2020-09-23 |
| 303 |
datetime.datetime(2021, 9, 23, 0, 0) |
| 304 |
|
| 305 |
>>> dateparser.parse('tomorrow', settings={'RELATIVE_BASE': datetime.datetime(1992, 1, 1)}) |
| 306 |
datetime.datetime(1992, 1, 2, 0, 0) |
| 307 |
|
| 308 |
To see all available settings, check the `settings |
| 309 |
documentation <https://dateparser.readthedocs.io/en/latest/settings.html>`__. |
| 310 |
|
| 311 |
False positives |
| 312 |
^^^^^^^^^^^^^^^ |
| 313 |
|
| 314 |
**dateparser** will do its best to return a date, dealing with multiple formats and different locales. |
| 315 |
For that reason it is important that the input is a valid date, otherwise it could return false positives. |
| 316 |
|
| 317 |
To reduce the possibility of receiving false positives, make sure that: |
| 318 |
|
| 319 |
- The input string is a valid date and doesn't contain any other words or numbers. |
| 320 |
- If you know the language or languages beforehand, you add them through the ``languages`` or ``locales`` properties. |
| 321 |
|
| 322 |
|
| 323 |
On the other hand, if you want to exclude any of the default parsers |
| 324 |
(``timestamp``, ``relative-time``...) or change the order in which they |
| 325 |
are executed, you can do so through the |
| 326 |
`settings PARSERS <https://dateparser.readthedocs.io/en/latest/usage.html#handling-incomplete-dates>`_. |
| 327 |
|
| 328 |
Installation |
| 329 |
------------ |
| 330 |
|
| 331 |
Dateparser supports Python 3.10+. You can install it by doing: |
| 332 |
|
| 333 |
:: |
| 334 |
|
| 335 |
$ pip install dateparser |
| 336 |
|
| 337 |
If you want to use the jalali or hijri calendar, you need to install the |
| 338 |
``calendars`` extra: |
| 339 |
|
| 340 |
:: |
| 341 |
|
| 342 |
$ pip install dateparser[calendars] |
| 343 |
|
| 344 |
Supported Calendars |
| 345 |
------------------- |
| 346 |
|
| 347 |
Apart from the Gregorian calendar, `dateparser` supports the |
| 348 |
`Persian Jalali calendar` and the `Hijri/Islamic calendar`. |
| 349 |
To use them, install the ``calendars`` extra (see `Installation`_). |
| 350 |
|
| 351 |
Example using the `Persian Jalali calendar |
| 352 |
<https://en.wikipedia.org/wiki/Iranian_calendars#Zoroastrian_calendar>`_: |
| 353 |
|
| 354 |
.. code:: python |
| 355 |
|
| 356 |
>>> from dateparser.calendars.jalali import JalaliCalendar |
| 357 |
>>> JalaliCalendar('جمعه سی ام اسفند ۱۳۸۷').get_date() |
| 358 |
DateData(date_obj=datetime.datetime(2009, 3, 20, 0, 0), period='day', locale=None) |
| 359 |
|
| 360 |
Example using the `Hijri/Islamic calendar |
| 361 |
<https://en.wikipedia.org/wiki/Islamic_calendar>`_: |
| 362 |
|
| 363 |
.. code:: python |
| 364 |
|
| 365 |
>>> from dateparser.calendars.hijri import HijriCalendar |
| 366 |
>>> HijriCalendar('17-01-1437 هـ 08:30 مساءً').get_date() |
| 367 |
DateData(date_obj=datetime.datetime(2015, 10, 30, 20, 30), period='day', locale=None) |
| 368 |
|
| 369 |
Dependencies |
| 370 |
------------ |
| 371 |
|
| 372 |
`dateparser` relies on the following libraries: |
| 373 |
|
| 374 |
* dateutil_'s module ``relativedelta`` for its freshness parser. |
| 375 |
* convertdate_ to convert *Jalali* dates to *Gregorian*. |
| 376 |
* hijridate_ to convert *Hijri* dates to *Gregorian*. |
| 377 |
* tzlocal_ to reliably get local timezone. |
| 378 |
* ruamel.yaml_ (optional) for operations on language files. |
| 379 |
|
| 380 |
.. _dateutil: https://pypi.python.org/pypi/python-dateutil |
| 381 |
.. _convertdate: https://pypi.python.org/pypi/convertdate |
| 382 |
.. _hijridate: https://pypi.python.org/pypi/hijridate |
| 383 |
.. _tzlocal: https://pypi.python.org/pypi/tzlocal |
| 384 |
.. _ruamel.yaml: https://pypi.python.org/pypi/ruamel.yaml |
| 385 |
|
| 386 |
Common use cases |
| 387 |
---------------- |
| 388 |
|
| 389 |
**dateparser** can be used for a wide variety of purposes, |
| 390 |
but it stands out when it comes to: |
| 391 |
|
| 392 |
Consuming data from different sources: |
| 393 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 394 |
|
| 395 |
- **Scraping**: extract dates from different places with several |
| 396 |
different formats and languages |
| 397 |
- **IoT**: consuming data coming from different sources with different |
| 398 |
date formats |
| 399 |
- **Tooling**: consuming dates from different logs / sources |
| 400 |
- **Format transformations**: when transforming dates coming from |
| 401 |
different files (PDF, CSV, etc.) to other formats (database, etc). |
| 402 |
|
| 403 |
Offering natural interaction with users: |
| 404 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 405 |
|
| 406 |
- **Tooling and CLI**: allow users to write “3 days ago” to retrieve |
| 407 |
information. |
| 408 |
- **Search engine**: allow people to search by date in an easy / |
| 409 |
natural format. |
| 410 |
- **Bots**: allow users to interact with a bot easily |
| 411 |
|
| 412 |
You may also like... |
| 413 |
-------------------- |
| 414 |
|
| 415 |
- `price-parser <https://github.com/scrapinghub/price-parser/>`__ - A |
| 416 |
small library for extracting price and currency from raw text |
| 417 |
strings. |
| 418 |
- `number-parser <https://github.com/scrapinghub/number-parser/>`__ - |
| 419 |
Library to convert numbers written in the natural language to it's |
| 420 |
equivalent numeric forms. |
| 421 |
- `Scrapy <https://github.com/scrapy/scrapy/>`__ - Web crawling and web |
| 422 |
scraping framework |
| 423 |
|
| 424 |
License |
| 425 |
------- |
| 426 |
|
| 427 |
`BSD3-Clause <https://github.com/scrapinghub/dateparser/blob/master/LICENSE>`__ |