Formatting new date time instances


(Taffyb) #1

Cypher now has great support for creating and comparing date time instances. Can we expect to see new functions for formatting these?


(M. David Allen) #2

Have you had a look at the APOC date parsing/formatting functions?

https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_conversion_between_formatted_dates_and_timestamps


(Bajal) #3

Hi taffyb,

You should be able to use the apoc library methods: apoc.date.parse() and apoc.date.format() to achieve what you are looking for.

As an example, to convert the current timestamp in epoch that is returned by timestamp() function to a friendly format, you could do

WITH apoc.date.format( timestamp(), 'yyyyMMdd HH:mm:ss.ms') AS initialTime RETURN initialTime

Documentation can be found here: https://neo4j-contrib.github.io/neo4j-apoc-procedures/


(Taffyb) #4

Yes I have used apoc to do this. However currently (as far as I can tell) it requires me to get the timestamp or epoch value then format. I was hoping there might be something a bit more native.


(Bajal) #5

You can declare dates in UTC format and get it back as epoch as well using datetime().

WITH datetime('2015-07-21T21:40:32.142+0100') AS date1 RETURN date1.epochMillis

I am not sure what your exact use-case is, but to feed a date into a query you have to use one of these formats:

  1. epoch millis
  2. UTC date string
  3. A full date object, specifiying the timezone information:
WITH datetime({ year:1984, month:11, day:11, hour:12, minute:31, second:14, nanosecond: 645876123, timezone:'Europe/Stockholm' })

(Andrew Bowman) #6

Hi taffyb,

This is something we'd like to see for our native temporal types, as the date formatting for APOC is convenient.

I believe this is on our backlog, though I'll confirm (and make sure it is if not).


(Taffyb) #7

Andrew
Native formating seems to be a natural addition. However changes to the apoc routines to accept temporal types as input might be another approach.