Do you want to try it out without installing any dependency? Now you can test
it quickly by visiting this online demo!
How To Use
The most straightforward way to parse dates with dateparser is to
use the dateparser.parse() function, that wraps around most of the
functionality of the module.
As you can see, dateparser works with different date formats, but it
can also be used directly with strings in different languages:
>>>dateparser.parse('Martes 21 de Octubre de 2014') # Spanish (Tuesday 21 October 2014)datetime.datetime(2014, 10, 21, 0, 0)
>>>dateparser.parse('Le 11 Décembre 2014 à 09:00') # French (11 December 2014 at 09:00)datetime.datetime(2014, 12, 11, 9, 0)
>>>dateparser.parse('13 января 2015 г. в 13:34') # Russian (13 January 2015 at 13:34)datetime.datetime(2015, 1, 13, 13, 34)
>>>dateparser.parse('1 เดือนตุลาคม 2005, 1:00 AM') # Thai (1 October 2005, 1:00 AM)datetime.datetime(2005, 10, 1, 1, 0)
>>>dateparser.parse('yaklaşık 23 saat önce') # Turkish (23 hours ago), current time: 12:46datetime.datetime(2019, 9, 7, 13, 46)
>>>dateparser.parse('2小时前') # Chinese (2 hours ago), current time: 22:30datetime.datetime(2018, 5, 31, 20, 30)
You can control multiple behaviors by using the settings parameter:
To see more examples on how to use the settings, check the settings
section
in the docs.
False positives
dateparser will do its best to return a date, dealing with multiple formats and different locales.
For that reason it is important that the input is a valid date, otherwise it could return false positives.
To reduce the possibility of receiving false positives, make sure that:
The input string it's a valid date and it doesn't contain any other words or numbers.
If you know the language or languages beforehand you add them through the languages or locales properties.
On the other hand, if you want to exclude any of the default parsers
(timestamp, relative-time...) or change the order in which they
are executed, you can do so through the
settings PARSERS.
Installation
Dateparser supports Python >= 3.5. You can install it by doing:
$ pip install dateparser
If you want to use the jalali or hijri calendar, you need to install the
calendars extra:
$ pip install dateparser[calendars]
Common use cases
dateparser can be used with a really different number of purposes,
but it stands out when it comes to:
Consuming data from different sources:
Scraping: extract dates from different places with several
different formats and languages
IoT: consuming data coming from different sources with different
date formats
Tooling: consuming dates from different logs / sources
Format transformations: when transforming dates coming from
different files (PDF, CSV, etc.) to other formats (database, etc).
Offering natural interaction with users:
Tooling and CLI: allow users to write “3 days ago” to retrieve
information.
Search engine: allow people to search by date in an easiest /
natural format.
Bots: allow users to interact with a bot easily
You may also like...
price-parser - A
small library for extracting price and currency from raw text
strings.
number-parser -
Library to convert numbers written in the natural language to it's
equivalent numeric forms.
请发表评论