| 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 |
<h1 align="center"> |
| 6 |
<br/> |
| 7 |
<a href="https://github.com/scrapinghub/dateparser"> |
| 8 |
<img src="https://raw.githubusercontent.com/scrapinghub/dateparser/master/artwork/dateparser-logo.png" alt="Dateparser" width="500"> |
| 9 |
</a> |
| 10 |
<br/> |
| 11 |
</h1> |
| 12 |
|
| 13 |
<h3 align="center">Python parser for human readable dates</h4> |
| 14 |
|
| 15 |
<p align="center"> |
| 16 |
<a href="https://pypi.python.org/pypi/dateparser"> |
| 17 |
<img src="https://img.shields.io/pypi/dm/dateparser.svg" alt="PyPI - Downloads"> |
| 18 |
</a> |
| 19 |
<a href="https://pypi.python.org/pypi/dateparser"> |
| 20 |
<img src="https://img.shields.io/pypi/v/dateparser.svg" alt="PypI - Version"> |
| 21 |
</a> |
| 22 |
<a href="https://codecov.io/gh/scrapinghub/dateparser"> |
| 23 |
<img src="https://codecov.io/gh/scrapinghub/dateparser/branch/master/graph/badge.svg" alt="Code Coverage"> |
| 24 |
</a> |
| 25 |
<a href="https://github.com/scrapinghub/dateparser/actions"> |
| 26 |
<img src="https://github.com/scrapinghub/dateparser/workflows/Build/badge.svg" alt="Github - Build"> |
| 27 |
</a> |
| 28 |
<a href="https://dateparser.readthedocs.org/en/latest/?badge=latest"> |
| 29 |
<img src="https://readthedocs.org/projects/dateparser/badge/?version=latest" alt="Readthedocs - Docs"> |
| 30 |
</a> |
| 31 |
</p> |
| 32 |
|
| 33 |
<p align="center"> |
| 34 |
<a href="#key-features">Key Features</a> • |
| 35 |
<a href="#how-to-use">How To Use</a> • |
| 36 |
<a href="#installation">Installation</a> • |
| 37 |
<a href="#common-use-cases">Common use cases</a> • |
| 38 |
<a href="#you-may-also-like">You may also like...</a> • |
| 39 |
<a href="#license">License</a> |
| 40 |
</p> |
| 41 |
|
| 42 |
|
| 43 |
Key Features |
| 44 |
------------ |
| 45 |
|
| 46 |
- Support for almost every existing date format: absolute dates, |
| 47 |
relative dates (``"two weeks ago"`` or ``"tomorrow"``), timestamps, |
| 48 |
etc. |
| 49 |
- Support for more than `200 language |
| 50 |
locales <https://dateparser.readthedocs.io/en/latest/supported_locales.html>`__. |
| 51 |
- Language autodetection |
| 52 |
- Customizable behavior through |
| 53 |
`settings <https://dateparser.readthedocs.io/en/latest/settings.html>`__. |
| 54 |
- Support for `non-Gregorian calendar |
| 55 |
systems <https://dateparser.readthedocs.io/en/latest/introduction.html#supported-calendars>`__. |
| 56 |
- Support for dates with timezones abbreviations or UTC offsets |
| 57 |
(``"August 14, 2015 EST"``, ``"21 July 2013 10:15 pm +0500"``...) |
| 58 |
- `Search |
| 59 |
dates <https://dateparser.readthedocs.io/en/latest/introduction.html#search-for-dates-in-longer-chunks-of-text>`__ |
| 60 |
in longer texts. |
| 61 |
|
| 62 |
Online demo |
| 63 |
----------- |
| 64 |
|
| 65 |
Do you want to try it out without installing any dependency? Now you can test |
| 66 |
it quickly by visiting `this online demo <https://dateparser-demo.netlify.app/>`__! |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
How To Use |
| 71 |
---------- |
| 72 |
|
| 73 |
The most straightforward way to parse dates with **dateparser** is to |
| 74 |
use the ``dateparser.parse()`` function, that wraps around most of the |
| 75 |
functionality of the module. |
| 76 |
|
| 77 |
.. code:: python |
| 78 |
|
| 79 |
>>> import dateparser |
| 80 |
|
| 81 |
>>> dateparser.parse('Fri, 12 Dec 2014 10:55:50') |
| 82 |
datetime.datetime(2014, 12, 12, 10, 55, 50) |
| 83 |
|
| 84 |
>>> dateparser.parse('1991-05-17') |
| 85 |
datetime.datetime(1991, 5, 17, 0, 0) |
| 86 |
|
| 87 |
>>> dateparser.parse('In two months') # today is 1st Aug 2020 |
| 88 |
datetime.datetime(2020, 10, 1, 11, 12, 27, 764201) |
| 89 |
|
| 90 |
>>> dateparser.parse('1484823450') # timestamp |
| 91 |
datetime.datetime(2017, 1, 19, 10, 57, 30) |
| 92 |
|
| 93 |
>>> dateparser.parse('January 12, 2012 10:00 PM EST') |
| 94 |
datetime.datetime(2012, 1, 12, 22, 0, tzinfo=<StaticTzInfo 'EST'>) |
| 95 |
|
| 96 |
As you can see, **dateparser** works with different date formats, but it |
| 97 |
can also be used directly with strings in different languages: |
| 98 |
|
| 99 |
.. code:: python |
| 100 |
|
| 101 |
>>> dateparser.parse('Martes 21 de Octubre de 2014') # Spanish (Tuesday 21 October 2014) |
| 102 |
datetime.datetime(2014, 10, 21, 0, 0) |
| 103 |
|
| 104 |
>>> dateparser.parse('Le 11 Décembre 2014 à 09:00') # French (11 December 2014 at 09:00) |
| 105 |
datetime.datetime(2014, 12, 11, 9, 0) |
| 106 |
|
| 107 |
>>> dateparser.parse('13 января 2015 г. в 13:34') # Russian (13 January 2015 at 13:34) |
| 108 |
datetime.datetime(2015, 1, 13, 13, 34) |
| 109 |
|
| 110 |
>>> dateparser.parse('1 เดือนตุลาคม 2005, 1:00 AM') # Thai (1 October 2005, 1:00 AM) |
| 111 |
datetime.datetime(2005, 10, 1, 1, 0) |
| 112 |
|
| 113 |
>>> dateparser.parse('yaklaşık 23 saat önce') # Turkish (23 hours ago), current time: 12:46 |
| 114 |
datetime.datetime(2019, 9, 7, 13, 46) |
| 115 |
|
| 116 |
>>> dateparser.parse('2小时前') # Chinese (2 hours ago), current time: 22:30 |
| 117 |
datetime.datetime(2018, 5, 31, 20, 30) |
| 118 |
|
| 119 |
You can control multiple behaviors by using the ``settings`` parameter: |
| 120 |
|
| 121 |
.. code:: python |
| 122 |
|
| 123 |
>>> dateparser.parse('2014-10-12', settings={'DATE_ORDER': 'YMD'}) |
| 124 |
datetime.datetime(2014, 10, 12, 0, 0) |
| 125 |
|
| 126 |
>>> dateparser.parse('2014-10-12', settings={'DATE_ORDER': 'YDM'}) |
| 127 |
datetime.datetime(2014, 12, 10, 0, 0) |
| 128 |
|
| 129 |
>>> dateparser.parse('1 year', settings={'PREFER_DATES_FROM': 'future'}) # Today is 2020-09-23 |
| 130 |
datetime.datetime(2021, 9, 23, 0, 0) |
| 131 |
|
| 132 |
>>> dateparser.parse('tomorrow', settings={'RELATIVE_BASE': datetime.datetime(1992, 1, 1)}) |
| 133 |
datetime.datetime(1992, 1, 2, 0, 0) |
| 134 |
|
| 135 |
To see more examples on how to use the ``settings``, check the `settings |
| 136 |
section <https://dateparser.readthedocs.io/en/latest/settings.html>`__ |
| 137 |
in the docs. |
| 138 |
|
| 139 |
False positives |
| 140 |
^^^^^^^^^^^^^^^ |
| 141 |
|
| 142 |
**dateparser** will do its best to return a date, dealing with multiple formats and different locales. |
| 143 |
For that reason it is important that the input is a valid date, otherwise it could return false positives. |
| 144 |
|
| 145 |
To reduce the possibility of receiving false positives, make sure that: |
| 146 |
|
| 147 |
- The input string is a valid date and doesn't contain any other words or numbers. |
| 148 |
- If you know the language or languages beforehand, you add them through the ``languages`` or ``locales`` properties. |
| 149 |
|
| 150 |
|
| 151 |
On the other hand, if you want to exclude any of the default parsers |
| 152 |
(``timestamp``, ``relative-time``...) or change the order in which they |
| 153 |
are executed, you can do so through the |
| 154 |
`settings PARSERS <https://dateparser.readthedocs.io/en/latest/usage.html#handling-incomplete-dates>`_. |
| 155 |
|
| 156 |
Installation |
| 157 |
------------ |
| 158 |
|
| 159 |
Dateparser supports Python >= 3.9. You can install it by doing: |
| 160 |
|
| 161 |
:: |
| 162 |
|
| 163 |
$ pip install dateparser |
| 164 |
|
| 165 |
If you want to use the jalali or hijri calendar, you need to install the |
| 166 |
``calendars`` extra: |
| 167 |
|
| 168 |
:: |
| 169 |
|
| 170 |
$ pip install dateparser[calendars] |
| 171 |
|
| 172 |
Common use cases |
| 173 |
---------------- |
| 174 |
|
| 175 |
**dateparser** can be used for a wide variety of purposes, |
| 176 |
but it stands out when it comes to: |
| 177 |
|
| 178 |
Consuming data from different sources: |
| 179 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 180 |
|
| 181 |
- **Scraping**: extract dates from different places with several |
| 182 |
different formats and languages |
| 183 |
- **IoT**: consuming data coming from different sources with different |
| 184 |
date formats |
| 185 |
- **Tooling**: consuming dates from different logs / sources |
| 186 |
- **Format transformations**: when transforming dates coming from |
| 187 |
different files (PDF, CSV, etc.) to other formats (database, etc). |
| 188 |
|
| 189 |
Offering natural interaction with users: |
| 190 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 191 |
|
| 192 |
- **Tooling and CLI**: allow users to write “3 days ago” to retrieve |
| 193 |
information. |
| 194 |
- **Search engine**: allow people to search by date in an easy / |
| 195 |
natural format. |
| 196 |
- **Bots**: allow users to interact with a bot easily |
| 197 |
|
| 198 |
You may also like... |
| 199 |
-------------------- |
| 200 |
|
| 201 |
- `price-parser <https://github.com/scrapinghub/price-parser/>`__ - A |
| 202 |
small library for extracting price and currency from raw text |
| 203 |
strings. |
| 204 |
- `number-parser <https://github.com/scrapinghub/number-parser/>`__ - |
| 205 |
Library to convert numbers written in the natural language to it's |
| 206 |
equivalent numeric forms. |
| 207 |
- `Scrapy <https://github.com/scrapy/scrapy/>`__ - Web crawling and web |
| 208 |
scraping framework |
| 209 |
|
| 210 |
License |
| 211 |
------- |
| 212 |
|
| 213 |
`BSD3-Clause <https://github.com/scrapinghub/dateparser/blob/master/LICENSE>`__ |