Conversions

A value becomes a date only through an explicit step; nothing is converted by guessing (a string that looks like a date stays a string).

as_date(): TinkerPop's asDate()

as_date() converts to a datetime exactly as TinkerPop's asDate() does:

IncomingResult
a datetimeunchanged
a stringread leniently as ISO 8601 / RFC 3339: without an offset it is UTC, a date alone is midnight UTC (Text Formats)
an int64milliseconds since the epoch, in UTC
a date, a localdatetimethe datetime at UTC (Graphersal: TinkerPop has no such types)
anything else (a float64, (), a list, a map, a time, a duration, an element)an error starting with "Can't parse", as in TinkerPop
g.inject("2023-08-02T09:30:00+02:00", 1690000000000, date("2023-08-02")).as_date().to_list()   // [2023-08-02T09:30:00+02:00, 2023-07-22T04:26:40Z, 2023-08-02T00:00:00Z]
g.inject(1.5).as_date().next()   // error: Can't parse float64 1.5 as a datetime

as_date(GType.X): to another date type

as_date(GType.DATETIME | DATE | LOCALDATETIME | TIME | OFFSETTIME) converts to that type (a Graphersal extension, spelled like as_number(GType.LONG)):

Incoming →DATETIMEDATELOCALDATETIMETIMEOFFSETTIME
stringlenient, as as_date()strict: a date textstrict: a local date-time textstrict: a time textstrict: a time text with its offset
datetimeunchangedits wall-clock date (at its own offset)its wall clockits wall-clock timeits wall-clock time and offset
localdatetimeat UTCits dateunchangedits timeits time at UTC
datemidnight UTCunchangedmidnightan erroran error
timean erroran erroran errorunchangedat UTC
offsettimean erroran erroran errorits local timeunchanged
int64epoch milliseconds, UTCan erroran erroran erroran error
g.inject(datetime("2023-08-02T09:30+02:00")).as_date(GType.OFFSETTIME).next()   // offsettime("09:30:00+02:00")
g.inject(offsettime("09:30+02:00")).as_date(GType.TIME).next()   // time("09:30:00")

GType.DURATION is not a target here: use as_duration(). To read a datetime's wall clock in another zone, use as_local(zone) first (Time Zones).

g.inject(datetime("2023-08-02T23:30-07:00")).as_date(GType.DATE).next()   // date("2023-08-02")
g.inject(datetime("2023-08-02T23:30-07:00")).as_local("UTC").as_date(GType.DATE).next()   // date("2023-08-03")
g.inject("2023-08-02").as_date(GType.DATE).next()   // date("2023-08-02")
g.inject("2023-08-02T09:30").as_date(GType.DATE).next()   // error: unexpected text after the value

as_duration()

as_duration() reads an ISO 8601 duration text; a duration passes unchanged, anything else fails with "Can't parse".

g.inject("PT90M").as_duration().next()   // duration("PT1H30M")

To numbers and text

  • as_number() of a datetime is its epoch milliseconds (TinkerPop), rounded down; the other date types are not numbers (use date_part(DT.epoch) or date_diff()).
  • as_string() of any date is its canonical text; in a script, x.as_string() does the same.
  • cast(GType.DATETIME) (and the other date tokens) parses a string or, for DATETIME, reads an int64 as epoch milliseconds, like as_date(GType.X).
g.inject(datetime("2023-08-02T00:00Z")).as_number().next()   // 1690934400000
g.inject(datetime("2023-08-02T09:30+02:00")).as_string().next()   // "2023-08-02T09:30:00+02:00"
g.inject(date("2023-08-02")).as_number().next()   // error: Cast exception

Storing text as a date

A property written as a string stays a string. To store dates, declare the property in the schema ("format": "date-time"): in open and closed mode a string written to it is converted on write. Or convert in the query: property("at", __.constant(text).as_date()), or write a date value (property("on", date("2023-08-02"))).