0
import dateparser
print(dateparser.parse("1 week"))
print(dateparser.parse("1 month"))
print(dateparser.parse("6 months"))
print(dateparser.parse("1 year"))

The above code lines return the below output:

2022-01-27 11:30:50.229535
2022-01-03 11:30:50.231393
2021-08-03 11:30:50.232393
2021-02-03 11:30:50.232938

Essentially they consider the current timestamp and return prev one week or prev one moth or prev 6 months or prev one year date time. What if I want date parser to consider a custom timestamp and based on that return the prev one week or prev one moth or prev 6 months or prev one year date time?? For Example: For the same above code, I want the below output because I want date parser to consider the start date as 2021-06-13 11:30:50.229535

2022-06-06 11:30:50.229535
2022-05-14 11:30:50.231393
2021-01-13 11:30:50.232393
2020-06-13 11:30:50.232938

how to go about configuring this start date?

liteyagami
  • 47
  • 5

1 Answers1

0

You can define that in the settings dict, via the RELATIVE_BASE key.

From the docs:

RELATIVE_BASE: allows setting the base datetime to use for interpreting partial or relative date strings. Defaults to the current date and time.

Ex:

from datetime import datetime
import dateparser

settings={'RELATIVE_BASE': datetime.fromisoformat("2021-06-13 11:30:50.229535")}

print(dateparser.parse("1 week", settings=settings))
print(dateparser.parse("1 month", settings=settings))
print(dateparser.parse("6 months", settings=settings))
print(dateparser.parse("1 year", settings=settings))
# 2021-06-06 11:30:50.229535
# 2021-05-13 11:30:50.229535
# 2020-12-13 11:30:50.229535
# 2020-06-13 11:30:50.229535
FObersteiner
  • 22,500
  • 8
  • 42
  • 72