DateTime
Chapter lab: Text and data
This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.
Or fetch and unpack it from a terminal in VS Code:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch15-text.zip && unzip -q ch15-text.zip && rm ch15-text.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch15-text.zip -OutFile ch15-text.zip; Expand-Archive ch15-text.zip -DestinationPath .; Remove-Item ch15-text.zipOpen the folder in VS Code with code rfe-ch15-text (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:
uv syncpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython -m venv .venv.venv\Scripts\Activate.ps1pip install -r requirements.txtKeep the environment active in every terminal where you run lesson commands.
poetry install --no-rootPython 3.13 or newer is required; uv installs it when it is missing.
Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.
What's inside
Directoryrfe-ch15-text/
Directory.vscode/
- extensions.json
Directorychecks/
- smoke.robot
Directorysample_data/
- app.log
- catalog.xml
- orders.csv
- results.xml
- settings.cfg
- .gitignore
- .python-version
- LAB.md
- pyproject.toml
- README.md
- requirements.txt
- uv.lock
The DateTime standard library parses dates and computes durations — useful
whenever a check involves timing or timestamps. Convert Date parses a date
string; Subtract Date From Date returns the gap, in seconds by default.
Task 1 — Compute a duration between two times
Section titled “Task 1 — Compute a duration between two times”Goal: create checks/datetime.robot that parses two times twenty minutes
apart and asserts the gap between them in seconds.
Here is the same calculation over a different, shorter interval. Adapt it.
Worked example — adapt it, don’t copy it
*** Settings ***Library DateTime
*** Test Cases ***Measure Minutes Between Times ${start}= Convert Date 2026-01-15 09:00:00 ${end}= Convert Date 2026-01-15 09:05:00 ${gap}= Subtract Date From Date ${end} ${start} Should Be Equal As Numbers ${gap} 300Done when
the suite imports DateTime and defines Compute A Duration.
Hint 1
Parse both times with Convert Date, e.g.
Convert Date 2026-07-01 08:00:00 and Convert Date 2026-07-01 08:20:00.
Hint 2
Subtract Date From Date ${end} ${start} gives the gap in seconds. Twenty
minutes is 1200 — assert Should Be Equal As Numbers ${elapsed} 1200.
Solution
*** Settings ***Library DateTime
*** Test Cases ***Compute A Duration ${start}= Convert Date 2026-07-01 08:00:00 ${end}= Convert Date 2026-07-01 08:20:00 ${elapsed}= Subtract Date From Date ${end} ${start} Should Be Equal As Numbers ${elapsed} 1200Task 2 — Run it green
Section titled “Task 2 — Run it green”Run:
uv run robot checksrobot checkspoetry run robot checksDone when
the computed duration is 1200 seconds.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
Subtract Date From Datereturns, by default…By default it returns the difference as a number of seconds.
Show the answer
Answer: the difference in seconds — By default it returns the difference as a number of seconds.
Convert Dateis used to…It converts a date string into a value you can compute with.
Show the answer
Answer: parse a date string into a comparable value — It converts a date string into a value you can compute with.
Key terms
- DateTimeconcept
- Standard library for dates and durations.
- Subtract Date From Datekeyword
- Difference between two dates, in seconds by default.