1 year ago

#176600

test-img

robertspierre

Know which parts a day contains

The dateparser library set missing parts of a date to today's values.

Example:

>>> import dateparser
>>> dateparser.parse("2015")
datetime.datetime(2015, 2, 14, 0, 0)

How to know which parts a date really contains?

(and thus which parts were set to today's values by the library)?

This is what I've come up with.

Is there a more efficient way?

date_str = input('Type a date: ')

settings = {"REQUIRE_PARTS": ["year"]}
res = dateparser.parse(date_str, settings=settings)
if res is None:
    print("Invalid Date")
    return

settings = {"REQUIRE_PARTS": ["year", "month"]}
res = dateparser.parse(date_str, settings=settings)
if res is None:
    print("Date has year only")
    return
    
settings = {"REQUIRE_PARTS": ["year", "month", "day"]}
res = dateparser.parse(date_str, settings=settings)
if res is None:
    print("Date has year and month")
    return

print("Date has year, month and day")

python

date

datetime

date-parsing

dateparser

0 Answers

Your Answer

Accepted video resources