Temporal (Date/Time) values
Refer to Temporal functions - instant types for information regarding temporal functions allowing for the creation and manipulation of temporal values. Refer to Temporal operators for information regarding temporal operators. Refer to Ordering and comparison of values for information regarding the comparison and ordering of temporal values. |
Introduction
The following table depicts the temporal value types and supported components:
Type | Date support | Time support | Time zone support |
---|---|---|---|
Date |
X |
||
Time |
X |
X |
|
LocalTime |
X |
||
DateTime |
X |
X |
X |
LocalDateTime |
X |
X |
|
Duration |
- |
- |
- |
Date, Time, LocalTime, DateTime and LocalDateTime are temporal instant types. A temporal instant value expresses a point in time with varying degrees of precision.
By contrast, Duration is not a temporal instant type. A Duration represents a temporal amount, capturing the difference in time between two instants, and can be negative. Duration only captures the amount of time between two instants, and thus does not encapsulate a start time and end time.
Time zones
Time zones are represented either as an offset from UTC, or as a logical identifier of a named time zone (these are based on the IANA time zone database). In either case the time is stored as UTC internally, and the time zone offset is only applied when the time is presented. This means that temporal instants can be ordered without taking time zone into account. If, however, two times are identical in UTC, then they are ordered by timezone.
When creating a time using a named time zone, the offset from UTC is computed from the rules in the time zone database to create a time instant in UTC, and to ensure the named time zone is a valid one.
It is possible for time zone rules to change in the IANA time zone database. For example, there could be alterations to the rules for daylight savings time in a certain area. If this occurs after the creation of a temporal instant, the presented time could differ from the originally-entered time, insofar as the local timezone is concerned. However, the absolute time in UTC would remain the same.
There are three ways of specifying a time zone in Cypher®:
-
Specifying the offset from UTC in hours and minutes (ISO 8601)
-
Specifying a named time zone
-
Specifying both the offset and the time zone name (with the requirement that these match)
The named time zone form uses the rules of the IANA time zone database to manage daylight savings time (DST).
The default time zone of the database can be configured using the configuration option db.temporal.timezone
.
This configuration option influences the creation of temporal types for the following functions:
-
Getting the current date and time without specifying a time zone.
-
Creating a temporal type from its components without specifying a time zone.
-
Creating a temporal type by parsing a string without specifying a time zone.
-
Creating a temporal type by combining or selecting values that do not have a time zone component, and without specifying a time zone.
-
Truncating a temporal value that does not have a time zone component, and without specifying a time zone.
Temporal instants
Specifying temporal instants
A temporal instant consists of three parts; the date
, the time
, and the timezone
.
These parts may then be combined to produce the various temporal value types.
Literal characters are denoted in bold
.
Temporal instant type | Composition of parts |
---|---|
Date |
|
Time |
|
LocalTime |
|
DateTime* |
|
LocalDateTime* |
|
*When date
and time
are combined, date
must be complete; i.e. fully identify a particular day.
Specifying dates
Component | Format | Description |
---|---|---|
Year |
|
Specified with at least four digits (special rules apply in certain cases) |
Month |
|
Specified with a double digit number from |
Week |
|
Always prefixed with |
Quarter |
|
Always prefixed with |
Day of the month |
|
Specified with a double digit number from |
Day of the week |
|
Specified with a single digit number from |
Day of the quarter |
|
Specified with a double digit number from |
Ordinal day of the year |
|
Specified with a triple digit number from |
If the year is before 0000
or after 9999
, the following additional rules apply:
-
-
must prefix any year before0000
-
+
must prefix any year after9999
-
The year must be separated from the next component with the following characters:
-
-
if the next component is month or day of the year -
Either
-
orW
if the next component is week of the year -
Q
if the next component is quarter of the year
-
If the year component is prefixed with either -
or +
, and is separated from the next component, Year
is allowed to contain up to nine digits.
Thus, the allowed range of years is between -999,999,999 and +999,999,999.
For all other cases, i.e. the year is between 0000
and 9999
(inclusive), Year
must have exactly four digits (the year component is interpreted as a year of the Common Era (CE)).
The following formats are supported for specifying dates:
Format | Description | Example | Interpretation of example |
---|---|---|---|
|
Calendar date: |
|
|
|
Calendar date: |
|
|
|
Calendar date: |
|
|
|
Calendar date: |
|
|
|
Week date: |
|
|
|
Week date: |
|
|
|
Week date: |
|
|
|
Week date: |
|
|
|
Quarter date: |
|
|
|
Quarter date: |
|
|
|
Quarter date: |
|
|
|
Quarter date: |
|
|
|
Ordinal date: |
|
|
|
Ordinal date: |
|
|
|
Year |
|
|
The least significant components can be omitted.
Cypher will assume omitted components to have their lowest possible value.
For example, 2013-06
will be interpreted as being the same date as 2013-06-01
.
Specifying times
Component | Format | Description |
---|---|---|
|
|
Specified with a double digit number from |
|
|
Specified with a double digit number from |
|
|
Specified with a double digit number from |
|
|
Specified with a number from |
Cypher does not support leap seconds; UTC-SLS (UTC with Smoothed Leap Seconds) is used to manage the difference in time between UTC and TAI (International Atomic Time).
The following formats are supported for specifying times:
Format | Description | Example | Interpretation of example |
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The least significant components can be omitted.
For example, a time may be specified with Hour
and Minute
, leaving out Second
and fraction
.
On the other hand, specifying a time with Hour
and Second
, while leaving out Minute
, is not possible.
Specifying time zones
The time zone is specified in one of the following ways:
-
As an offset from UTC
-
Using the
Z
shorthand for the UTC (±00:00
) time zone
When specifying a time zone as an offset from UTC, the rules below apply:
-
The time zone always starts with either a plus (
+
) or minus (-
) sign.-
Positive offsets, i.e. time zones beginning with
+
, denote time zones east of UTC. -
Negative offsets, i.e. time zones beginning with
-
, denote time zones west of UTC.
-
-
A double-digit hour offset follows the
+
/-
sign. -
An optional double-digit minute offset follows the hour offset, optionally separated by a colon (
:
). -
The time zone of the International Date Line is denoted either by
+12:00
or-12:00
, depending on country.
When creating values of the DateTime temporal instant type, the time zone may also be specified using a named time zone, using the names from the IANA time zone database.
This may be provided either in addition to, or in place of the offset.
The named time zone is given last and is enclosed in square brackets ([]
).
Should both the offset and the named time zone be provided, the offset must match the named time zone.
The following formats are supported for specifying time zones:
Format | Description | Example | Supported for DateTime |
Supported for Time |
---|---|---|---|---|
|
UTC |
|
X |
X |
|
|
|
X |
X |
|
|
|
X |
|
|
|
|
X |
X |
|
|
|
X |
|
|
|
|
X |
X |
|
|
|
X |
|
|
|
|
X |
Examples
We show below examples of parsing temporal instant values using various formats. For more details, refer to An overview of temporal instant type creation.
Parsing a DateTime using the calendar date format:
RETURN datetime('2015-06-24T12:50:35.556+0100') AS theDateTime
theDateTime |
---|
|
Rows: 1 |
Parsing a LocalDateTime using the ordinal date format:
RETURN localdatetime('2015185T19:32:24') AS theLocalDateTime
theLocalDateTime |
---|
|
Rows: 1 |
Parsing a Date using the week date format:
RETURN date('+2015-W13-4') AS theDate
theDate |
---|
|
Rows: 1 |
Parsing a Time:
RETURN time('125035.556+0100') AS theTime
theTime |
---|
|
Rows: 1 |
Parsing a LocalTime:
RETURN localtime('12:50:35.556') AS theLocalTime
theLocalTime |
---|
|
Rows: 1 |
Accessing components of temporal instants
Components of temporal instant values can be accessed as properties.
Component | Description | Type | Range/Format | Date | DateTime | LocalDateTime | Time | LocalTime |
---|---|---|---|---|---|---|---|---|
|
The |
Integer |
At least 4 digits. For more information, see the rules for using the |
X |
X |
X |
||
|
The quarter-of-the-year component. |
Integer |
|
X |
X |
X |
||
|
The month-of-the-year component. |
Integer |
|
X |
X |
X |
||
|
The week-of-the-year component.[2] |
Integer |
|
X |
X |
X |
||
|
The year that the week-of-year component belongs to.[3] |
Integer |
At least 4 digits. For more information, see the rules for using the |
X |
X |
X |
||
|
The day-of-the-quarter component. |
Integer |
|
X |
X |
X |
||
|
The day-of-the-quarter component. (alias for |
Integer |
|
X |
X |
X |
||
|
The day-of-the-month component. |
Integer |
|
X |
X |
X |
||
|
The day-of-the-year component. |
Integer |
|
X |
X |
X |
||
|
The day-of-the-week component (the first day of the week is Monday). |
Integer |
|
X |
X |
X |
||
|
The day-of-the-week component (alias for |
Integer |
|
X |
X |
X |
||
|
The hour component. |
Integer |
|
X |
X |
X |
X |
|
|
The minute component. |
Integer |
|
X |
X |
X |
X |
|
|
The second component. |
Integer |
|
X |
X |
X |
X |
|
|
The millisecond component. |
Integer |
|
X |
X |
X |
X |
|
|
The microsecond component. |
Integer |
|
X |
X |
X |
X |
|
|
The nanosecond component. |
Integer |
|
X |
X |
X |
X |
|
|
The timezone component. |
String |
Depending on how the time zone was specified, this is either a time zone name or an offset from UTC in the format |
X |
X |
|||
|
The timezone offset. |
String |
|
X |
X |
|||
|
The timezone offset in minutes. |
Integer |
|
X |
X |
|||
|
The timezone offset in seconds. |
Integer |
|
X |
X |
|||
|
The number of milliseconds between |
Integer |
Positive for instants after and negative for instants before |
X |
||||
|
The number of seconds between |
Integer |
Positive for instants after and negative for instants before |
X |
||||
1. This is in accordance with the Gregorian calendar; i.e. years AD/CE start at year 1, and the year before that (year 1 BC/BCE) is 0, while year 2 BCE is -1 etc. 2. The first week of any year is the week that contains the first Thursday of the year, and thus always contains January 4. 3. For dates from December 29, this could be the next year, and for dates until January 3 this could be the previous year, depending on how week 1 begins.
4. The expression
5. For the nanosecond part of the epoch offset, the regular nanosecond component ( |
The following query shows how to extract the components of a Date value:
WITH date({year: 1984, month: 10, day: 11}) AS d
RETURN d.year, d.quarter, d.month, d.week, d.weekYear, d.day, d.ordinalDay, d.dayOfWeek, d.dayOfQuarter
d.year | d.quarter | d.month | d.week | d.weekYear | d.day | d.ordinalDay | d.dayOfWeek | d.dayOfQuarter |
---|---|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
Rows: 1 |
The following query shows how to extract the date related components of a DateTime value:
WITH datetime({
year: 1984, month: 11, day: 11,
hour: 12, minute: 31, second: 14, nanosecond: 645876123,
timezone: 'Europe/Stockholm'
}) AS d
RETURN d.year, d.quarter, d.month, d.week, d.weekYear, d.day, d.ordinalDay, d.dayOfWeek, d.dayOfQuarter
d.year | d.quarter | d.month | d.week | d.weekYear | d.day | d.ordinalDay | d.dayOfWeek | d.dayOfQuarter |
---|---|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
Rows: 1 |
The following query shows how to extract the time related components of a DateTime value:
WITH datetime({
year: 1984, month: 11, day: 11,
hour: 12, minute: 31, second: 14, nanosecond: 645876123,
timezone: 'Europe/Stockholm'
}) AS d
RETURN d.hour, d.minute, d.second, d.millisecond, d.microsecond, d.nanosecond
d.hour | d.minute | d.second | d.millisecond | d.microsecond | d.nanosecond |
---|---|---|---|---|---|
|
|
|
|
|
|
Rows: 1 |
The following query shows how to extract the epoch time and timezone related components of a DateTime value:
WITH datetime({
year: 1984, month: 11, day: 11,
hour: 12, minute: 31, second: 14, nanosecond: 645876123,
timezone: 'Europe/Stockholm'
}) AS d
RETURN d.timezone, d.offset, d.offsetMinutes, d.epochSeconds, d.epochMillis
d.timezone | d.offset | d.offsetMinutes | d.epochSeconds | d.epochMillis |
---|---|---|---|---|
|
|
|
|
|
Rows: 1 |
Durations
Specifying durations
A Duration represents a temporal amount, capturing the difference in time between two instants, and can be negative.
The specification of a Duration is prefixed with a P
, and can use either a unit-based form or a date-and-time-based form:
-
Unit-based form:
P
[n
Y
][n
M
][n
W
][n
D
][
T
[n
H
][n
M
][n
S
]]
-
The square brackets (
[]
) denote an optional component (components with a zero value may be omitted). -
The
n
denotes a numeric value which can be arbitrarily large. -
The value of the last — and least significant — component may contain a decimal fraction.
-
Each component must be suffixed by a component identifier denoting the unit.
-
The unit-based form uses
M
as a suffix for both months and minutes. Therefore, time parts must always be preceded withT
, even when no components of the date part are given.
-
-
Date-and-time-based form:
P
<date>
T
<time>
-
Unlike the unit-based form, this form requires each component to be within the bounds of a valid LocalDateTime.
-
The following table lists the component identifiers for the unit-based form:
Component identifier | Description | Comments |
---|---|---|
|
Years |
|
|
Months |
Must be specified before |
|
Weeks |
|
|
Days |
|
|
Hours |
|
|
Minutes |
Must be specified after |
|
Seconds |
Examples
The following examples demonstrate various methods of parsing Duration values. For more details, refer to Creating a Duration from a string.
Return a Duration of 14
days, 16
hours and 12
minutes:
RETURN duration('P14DT16H12M') AS theDuration
theDuration |
---|
|
Rows: 1 |
Return a Duration of 5
months, 1
day and 12
hours:
RETURN duration('P5M1.5D') AS theDuration
theDuration |
---|
|
Rows: 1 |
Return a Duration of 45
seconds:
RETURN duration('PT0.75M') AS theDuration
theDuration |
---|
|
Rows: 1 |
Return a Duration of 2
weeks, 3
days and 12
hours:
RETURN duration('P2.5W') AS theDuration
theDuration |
---|
|
Rows: 1 |
Accessing components of durations
A Duration can have several components, each categorized into Months, Days, and Seconds groups.
Components of Duration values are truncated within their component groups as follows:
Component Group | Component | Description | Type | Details |
---|---|---|---|---|
Months |
|
The total number of years |
Integer |
Each set of |
|
The total number of quarters |
Integer |
Each year is counted as |
|
|
The total number of months |
Integer |
Each year is counted as |
|
Days |
|
The total number of weeks |
Integer |
Each set of |
|
The total number of days |
Integer |
Each week is counted as |
|
Seconds |
|
The total number of hours |
Integer |
Each set of |
|
The total number of minutes |
Integer |
Each hour is counted as |
|
|
The total number of seconds |
Integer |
Each hour is counted as |
|
|
The total number of milliseconds |
Integer |
Each set of |
|
|
The total number of microseconds |
Integer |
Each millisecond is counted as |
|
|
The total number of nanoseconds |
Integer |
Each microsecond is counted as |
Please note that:
|
It is also possible to access the smaller (less significant) components of a component group bounded by the largest (most significant) component of the group:
Component | Component Group | Description | Type |
---|---|---|---|
|
Months |
The number of quarters in the group that do not make a whole year |
Integer |
|
Months |
The number of months in the group that do not make a whole year |
Integer |
|
Months |
The number of months in the group that do not make a whole quarter |
Integer |
|
Days |
The number of days in the group that do not make a whole week |
Integer |
|
Seconds |
The number of minutes in the group that do not make a whole hour |
Integer |
|
Seconds |
The number of seconds in the group that do not make a whole minute |
Integer |
|
Seconds |
The number of milliseconds in the group that do not make a whole second |
Integer |
|
Seconds |
The number of microseconds in the group that do not make a whole second |
Integer |
|
Seconds |
The number of nanoseconds in the group that do not make a whole second |
Integer |
The following query shows how to extract the month based components of a Duration value:
WITH duration({years: 1, months: 5, days: 111, minutes: 42}) AS d
RETURN d.years, d.quarters, d.quartersOfYear, d.months, d.monthsOfYear, d.monthsOfQuarter
d.years | d.quarters | d.quartersOfYear | d.months | d.monthsOfYear | d.monthsOfQuarter |
---|---|---|---|---|---|
|
|
|
|
|
|
Rows: 1 |
The following query shows how to extract the day based components of a Duration value:
WITH duration({months: 5, days: 25, hours: 1}) AS d
RETURN d.weeks, d.days, d.daysOfWeek
d.weeks | d.days | d.daysOfWeek |
---|---|---|
|
|
|
Rows: 1 |
The following query shows how to extract the most significant second based components of a Duration value:
WITH duration({
years: 1, months:1, days:1, hours: 1,
minutes: 1, seconds: 1, nanoseconds: 111111111
}) AS d
RETURN d.hours, d.minutes, d.seconds, d.milliseconds, d.microseconds, d.nanoseconds
d.hours | d.minutes | d.seconds | d.milliseconds | d.microseconds | d.nanoseconds |
---|---|---|---|---|---|
|
|
|
|
|
|
Rows: 1 |
The following query shows how to extract the less significant second based components of a Duration value:
WITH duration({
years: 1, months:1, days:1,
hours: 1, minutes: 1, seconds: 1, nanoseconds: 111111111
}) AS d
RETURN d.minutesOfHour, d.secondsOfMinute, d.millisecondsOfSecond, d.microsecondsOfSecond, d.nanosecondsOfSecond
d.minutesOfHour | d.secondsOfMinute | d.millisecondsOfSecond | d.microsecondsOfSecond | d.nanosecondsOfSecond |
---|---|---|---|---|
|
|
|
|
|
Rows: 1 |
Examples
The following examples illustrate the use of some of the temporal functions and operators. Refer to Temporal functions - instant types and Temporal operators for more details.
Create a Duration representing 1.5 days:
RETURN duration({days: 1, hours: 12}) AS theDuration
theDuration |
---|
|
Rows: 1 |
Compute the Duration between two temporal instants:
RETURN duration.between(date('1984-10-11'), date('2015-06-24')) AS theDuration
theDuration |
---|
|
Rows: 1 |
Compute the number of days between two Date values:
RETURN duration.inDays(date('2014-10-11'), date('2015-08-06')) AS theDuration
theDuration |
---|
|
Rows: 1 |
Get the first Date of the current year:
RETURN date.truncate('year') AS day
day |
---|
|
Rows: 1 |
Get the Date of the Thursday in the week of a specific date:
RETURN date.truncate('week', date('2019-10-01'), {dayOfWeek: 4}) AS thursday
thursday |
---|
|
Rows: 1 |
Get the Date of the last day of the next month:
RETURN date.truncate('month', date() + duration('P2M')) - duration('P1D') AS lastDay
lastDay |
---|
|
Rows: 1 |
Add a Duration to a Date:
RETURN time('13:42:19') + duration({days: 1, hours: 12}) AS theTime
theTime |
---|
|
Rows: 1 |
Add two Duration values:
RETURN duration({days: 2, hours: 7}) + duration({months: 1, hours: 18}) AS theDuration
theDuration |
---|
|
Rows: 1 |
Multiply a Duration by a number:
RETURN duration({hours: 5, minutes: 21}) * 14 AS theDuration
theDuration |
---|
|
Rows: 1 |
Divide a Duration by a number:
RETURN duration({hours: 3, minutes: 16}) / 2 AS theDuration
theDuration |
---|
|
Rows: 1 |
Examine whether two instants are less than one day apart:
WITH
datetime('2015-07-21T21:40:32.142+0100') AS date1,
datetime('2015-07-21T17:12:56.333+0100') AS date2
RETURN
CASE
WHEN date1 < date2 THEN date1 + duration("P1D") > date2
ELSE date2 + duration("P1D") > date1
END AS lessThanOneDayApart
lessThanOneDayApart |
---|
|
Rows: 1 |
Return the abbreviated name of the current month:
RETURN ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][date().month-1] AS month
month |
---|
|
Rows: 1 |