This page takes you from an empty directory to your first date read from text
and your first time written as words, in five steps. Each comment after a
print shows the output of a run in a fresh environment.
Make an environment and install the package. It needs no voice assistant.
uv venv
uv pip install --prerelease=allow ovos-date-parserRun each snippet of this page with uv run python, or with .venv/bin/python.
Save a snippet to a file and pass the file name, or paste it into the prompt.
extract_datetime gives the datetime that a phrase names and the words that
are left over. A phrase like "next friday" counts from anchorDate. Pass a
fixed anchor to get the same answer on every run.
from datetime import datetime
from ovos_date_parser import extract_datetime
anchor = datetime(2024, 1, 5)
when, leftover = extract_datetime("lets meet next friday at 8am", "en", anchorDate=anchor)
print(when) # 2024-01-12 08:00:00
print(leftover) # lets meetextract_duration gives a timedelta and the words that are left over.
from ovos_date_parser import extract_duration
delta, leftover = extract_duration("set a timer for 5 minutes", "en")
print(delta) # 0:05:00
print(leftover) # set a timernice_time, nice_date and nice_duration give words that a speech engine can
say.
from datetime import datetime
from ovos_date_parser import nice_time, nice_date, nice_duration
print(nice_time(datetime(2024, 1, 5, 15, 30), "en")) # half past three
print(nice_date(datetime(2024, 1, 5), "en")) # friday, january fifth, twenty twenty four
print(nice_duration(3690, "en")) # one hour one minute thirty secondsEach call takes a language code as its second argument. Reading and writing cover different sets of languages, and languages.md lists both.
from datetime import datetime
from ovos_date_parser import extract_datetime, nice_time
print(nice_time(datetime(2024, 1, 5, 15, 30), "de")) # halb vier
print(extract_datetime("amanhã às 15h30", "pt", anchorDate=datetime(2024, 1, 5))) # [datetime.datetime(2024, 1, 6, 15, 30), '']How it works shows which calls pass to chronologia and which code writes each language. The API reference lists every function with its parameters.