Zack Shapiro
Zack Shapiro

Reputation: 6998

Turning a localized Date to a Timestamp in Firebase

I'm new to Firestore and since it doesn't seem like it has a native createdAt/updatedAt as part of a document, I'm creating them when I create the new document. Using a straight up Date() as the value for my initial dictionary that I save to Firestore obviously gives me a localized date/time – UTC -500, for example.

Firestore stores this as November 19, 2018 at 5:14:54 PM UTC-5

Is there a specific date format that Firestore likes in order to save something as a Timestamp and so that I will be able to sort on it later?

I tried using "yyyy-MM-dd HH:mm:ss Z" as the dateFormat from just printing out the value of Date() in a Playground. Would love some help here. Thanks!

Upvotes: 0

Views: 194

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317427

A Timestamp object doesn't have a date format. It's just a measurement of a number of seconds since Unix epoch, plus some number of nanoseconds. It does not accept a formatted time. If you have a formatted time, you will have to parse it to get a Timestamp object in return.`

The date format you're seeing in the console is just the way the console is choosing to format it. If you want to format a Timestamp for display, you'll need to do that yourself.

Upvotes: 1

Related Questions