-1

I am using dateparser and i have got a case, when in the string with date there are other words and I have detected that it doesn't return expected result in this case.

from dateparser import parse

print(parse('April 19, 2006')) # returns 2006-04-19 00:00:00
print(parse('April 19, 2006 test test')) # returns None

How it works?

2 Answers2

0

As documentation has already warned

Support for searching dates is really limited and needs a lot of improvement, we look forward to community’s contribution to get better on that part

it's better not to use it that way. but there's a function for this case. try

from dateparser.search import search_dates
print(search_dates('April 19, 2006 test test'))

output:

[('April 19, 2006', datetime.datetime(2006, 4, 19, 0, 0))]
Ali Ent
  • 1,420
  • 6
  • 17
0

In your example, parse('April 19, 2006') returns a datetime object representing the date and time April 19, 2006 at 00:00:00. This is because the input string 'April 19, 2006' contains a well-formatted date and does not contain any extraneous information that could confuse the parsing algorithm.

However, when you pass the string April 19, 2006 test test to the parse function, it returns None because the string contains additional text that is not part of a valid date format.

If you want to parse a string that includes additional text, you can use the dateutil module instead:

from dateutil import parser

print(parser.parse('April 19, 2006')) # returns 2006-04-19 00:00:00
print(parser.parse('April 19, 2006 test test')) # returns 2006-04-19 00:00:00
Insula
  • 999
  • 4
  • 10