I’m hitting my faunadb from a nodejs client using the fauna javascript driver. My result object returned from faunadb includes a fauna Time object e.g. Time("2022-03-18T19:10:56.720272Z")
How do I properly convert this to either a javascript Date object or just an ISO string? I’m having a surprisingly hard time figuring this out (new to Fauna).
Among other things I’ve tried q.ToString()
but that just returns e.g.
Expr { raw: { to_string: Time("2022-03-28T16:49:00.741288Z") } }
(I assume because it’s just constructing a query object not actually executing said query?)
So I’m clearly not grokking something here. I don’t want to hit my fauna back-end again just to convert some object, and I assume there’s a way within the javascript driver to convert Fauna Time/Date objects into their javascript equivalents? I’m coming up short in the documentation though!
Thanks
Continuing the discussion from How to convert fauna Time object to nodejs Date object?:
There are multiple ways to do it and it depends on how you actually want to work with it and what fits you need best.
for example, if you save something in fauna like:
...
data.created = q.Now()
you can use it directly and just pass it into new Date
let ts = faunaObject.data.created?.["@ts"];
let time = ts ? new Date(ts).toLocaleString() : undefined;
return time;
hope that helps
2 Likes
Sure does. I was making it unnecessarily complicated. Didn’t realize you can just extract the ISO string directly!
With the JS Driver you can you use the date
getter for the FaunaTime
and FaunaDate
types.
For example:
const faunaTime = await client.query(q.Now())
const jsDate = faunaTime.date
So, you do not have to read the wire-protocol directly (the @ts
part). It is recommended that you don’t directly use the wire-protocol, or AST, that underlines FQL. It is an internal implementation that is subject to change while the driver API remains the same.
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.