Temporal Functions

These functions can be used to format temporal values using a valid DateTimeFormatter pattern.

Formatting Temporal Types

You can pass through any temporal type (Date, Time, DateTime, LocalTime, LocalDateTime, Duration) along with a pattern. Please note that if the pattern is invalid for the value that you pass in (for example HH for hours on a Date value or DD for day on a Time value), an Exception will be thrown.

Format as date
RETURN apoc.temporal.format( date(), 'YYYY-MM-dd') AS output;
Table 1. Results
Output

"2019-06-24"

Format as date and time
RETURN apoc.temporal.format( datetime(), 'YYYY-MM-dd HH:mm:ss.SSSSZ') AS output;
Table 2. Results
Output

"2019-06-24 13:56:56.8550+0000"

Format as time
RETURN apoc.temporal.format( localtime(), 'HH:mm:ss.SSSS') AS output;
Table 3. Results
Output

"13:57:31.9250"

You can also pass a ISO DATE TIME pattern, the list is available here ISO_DATE

For example:

Format as date
RETURN apoc.temporal.format( date( { year: 2018, month: 12, day: 10 } ), 'date' ) as output;
Table 4. Results
Output

"2018-12-10"

Format as date and time
RETURN apoc.temporal.format( localdatetime( { year: 2018, month: 12, day: 10, hour: 12, minute: 34, second: 56, nanosecond: 123456789 } ), 'ISO_LOCAL_DATE_TIME' ) as output;
Table 5. Results
Output

"2018-12-10T12:34:56.123456789"

Format as date
RETURN apoc.temporal.format( date( { year: 2018, month: 12, day: 10 } ), 'ISO_DATE' ) as output;
Table 6. Results
Output

"2018-12-10"

Formatting Durations

When attempting to format a duration, the procedure will attempt to create a date (01/01/0000) and add the duration. This allows you to provide a consistent format as above.

Format as duration
RETURN apoc.temporal.format( duration.between( datetime.transaction(), datetime.realtime() ) , 'HH:mm:ss.SSSS') AS output;
Table 7. Results
Output

"00:00:00.0110"

To ZonedDateTime

You can pass a string to be converted as a 1st parameter, a pattern to convert the string into a ZonedDateTime as a 2nd parameter (default: 'yyyy-MM-dd HH:mm:ss'), and the timezone as a 3rd parameter (default: 'UTC').

For example, using the default pattern and timezone:

RETURN apoc.temporal.toZonedTemporal('2015-12-23 23:59:59') AS output;
Table 8. Results
Output

"2015-12-23T23:59:59[UTC]"

or:

RETURN apoc.temporal.toZonedTemporal('2012-12-23T23:59:59', "yyyy-MM-dd'T'HH:mm:ss", "Asia/Tokyo") AS output;
Table 9. Results
Output

"2012-12-23T23:59:59[Asia/Tokyo]"