<a id="dates-and-times-in-test-cases"></a>
<a id="test-case-datetimes"></a>
# Dates and Times in Test Cases

Date and time values should be specified using a format corresponding to the format of the Field. For example, a Field 'AESTDAT' with format 'dd MMM yyyy' and Field 'AESTTIM' with format 'HH:nn' should be entered like this:

```gherkin
When I enter data:
  | DataPoint        | Value       |
  | AE.AE.AESTDAT[0] | 10 JAN 2020 |
  | AE.AE.AESTTIM[0] | 16:30       |
Then I should see field "AE.AE.AESTDAT[0]" has value "10 JAN 2020"
And I should see field "AE.AE.AESTTIM[0]" has value "16:30"
```

<a id="partial-dates-and-times"></a>
## Partial dates and times

Partial dates and times should be entered using 'UN' and 'UNK' as in Rave:

```gherkin
When I enter data:
  | DataPoint        | Value       |
  | AE.AE.AESTDAT[0] | UN UNK 2020 |
  | AE.AE.AESTTIM[0] | 16:UN       |
Then I should see field "AE.AE.AESTDAT[0]" has value "UN UNK 2020"
And I should see field "AE.AE.AESTTIM[0]" has value "16:UN"
```

<a id="incomplete-dates-and-times"></a>
## Incomplete dates and times

Incomplete dates and times should be entered using underscores as placeholders for the missing parts of the date. TrialGrid will enter these as empty in Rave. Underscores should also be used in verification steps.

```gherkin
When I enter data:
  | DataPoint        | Value       |
  | AE.AE.AESTDAT[0] | __ ___ 2020 |
  | AE.AE.AESTTIM[0] | 16:__       |
Then I should see field "AE.AE.AESTDAT[0]" has value "__ ___ 2020"
And I should see field "AE.AE.AESTTIM[0]" has value "16:__"
```

> **Note:** Underscore characters in non-datetime fields will be entered into Rave.

<a id="current-date-and-time"></a>
## Current date and time

The current date and/or time can be used in Test Cases using the 'now' keyword, and can be formatted for use in Rave with the 'dataformat' keyword.

```gherkin
{% set today = now()|dataformat('dd MMM yyyy') %}
{% set time = now()|dataformat('hh:nn') %}

When I enter data:
  | DataPoint        | Value       |
  | AE.AE.AESTDAT[0] | {{ today }} |
  | AE.AE.AESTTIM[0] | {{ time }}  |

Then I should see field "AE.AE.AESTDAT[0]" has value "{{ today }}"
And I should see field "AE.AE.AESTTIM[0]" has value "{{ time }}"
```

When the above Test Case steps are run, the current date and time will be inserted into the steps:

```gherkin
When I enter data:
  | DataPoint        | Value       |
  | AE.AE.AESTDAT[0] | 26 JAN 2023 |
  | AE.AE.AESTTIM[0] | 21:03       |

Then I should see field "AE.AE.AESTDAT[0]" has value "26 JAN 2023"
And I should see field "AE.AE.AESTTIM[0]" has value "21:03"
```

> **Note:** The format string inside 'dataformat' should use Rave format codes.

> **Note:** Because the syntax for applying the dataformat to 'now()' includes a pipe character ('|'), it cannot be inserted directly into tables such as the data entry table.

> **Note:** 'now()' returns a Python datetime 'utcnow'. Python methods can be applied, see below for example.

> **Note:** Creating variables for the current date and time will give consistent values throughout. The following steps might have different time values when run:

```gherkin
{% set today = now()|dataformat('dd MMM yyyy') %}
{% set time = now()|dataformat('hh:nn') %}

When I enter data:
  | DataPoint        | Value       |
  | AE.AE.AESTDAT[0] | {{ today }} |
  | AE.AE.AESTTIM[0] | {{ time }}  |

Then I should see field "AE.AE.AESTDAT[0]" has value "{{ today }}"
And I should see field "AE.AE.AESTTIM[0]" has value "{{ now()|dataformat('hh:nn') }}"
```

> **See also:** Python documentation: [datetime](https://docs.python.org/3/library/datetime.html#)

<a id="offsets-from-current-date-and-time"></a>
## Offsets from current date and time

Date intervals can be added and substracted from the current date or time using the 'timedelta' keyword.

```gherkin
{% set today = now()|dataformat('dd MMM yyyy') %}
{% set tomorrow = (now() + timedelta(days=1))|dataformat('dd MMM yyyy') %}
{% set yesterday = (now() - timedelta(days=1))|dataformat('dd MMM yyyy') %}

{% set time = now()|dataformat('hh:nn') %}
{% set time_1_min = (now() + timedelta(minutes=1))|dataformat('hh:nn') %}
{% set time_5_min = (now() + timedelta(minutes=5))|dataformat('hh:nn') %}

When I enter data:
  | DataPoint        | Value           |
  | AE.AE.AESTDAT[0] | {{ today }}     |
  | AE.AE.AEENDAT[0] | {{ yesterday }} |

When I enter data:
  | DataPoint        | Value            |
  | AE.AE.AESTDAT[0] | {{ today }}      |
  | AE.AE.AESTTIM[0] | {{ time }}       |
  | AE.AE.AEENDAT[0] | {{ today }}      |
  | AE.AE.AEENTIM[0] | {{ time_5_min }} |
```

> **See also:** Python documentation: [timedelta](https://docs.python.org/3/library/datetime.html#timedelta-objects)
