Skip to content

DateTime

Chapter lab: Text and datach15-text.zip · 5 KB

This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.

Download ch15-text.zip

Or fetch and unpack it from a terminal in VS Code:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch15-text.zip && unzip -q ch15-text.zip && rm ch15-text.zip

Open 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:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 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”
Edit

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} 300

Done 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} 1200
Run

Run:

Terminal window
uv run robot checks
Terminal window
robot checks
Terminal window
poetry run robot checks

Done 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.

  1. Subtract Date From Date returns, by default…

    Show the answer

    Answer: the difference in seconds — By default it returns the difference as a number of seconds.

  2. Convert Date is used to…

    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.

All terms in the glossary →