admin管理员组

文章数量:1300041

I know how to write the server timestamp to Firestore as a date string:

doc_ref.set({ts: firebase.firestore.FieldValue.serverTimestamp()});

but how can I store its unix timestamp as a number instead? Is this even possible? I need to be able to mirror a subset of my Firestore documents in Algolia and I don't believe I can sort an Algolia index by timestamp strings.

I know how to write the server timestamp to Firestore as a date string:

doc_ref.set({ts: firebase.firestore.FieldValue.serverTimestamp()});

but how can I store its unix timestamp as a number instead? Is this even possible? I need to be able to mirror a subset of my Firestore documents in Algolia and I don't believe I can sort an Algolia index by timestamp strings.

Share asked Dec 31, 2017 at 4:31 VincentVincent 1,6511 gold badge12 silver badges24 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

It's not being stored as a string in the document - that's just how it serverTimestamp renders in the console. If you examine the ts field more closely, you'll see that it has a type of "timestamp". When you go to edit the value in the console by clicking it, you'll see the type a value clearly shown as a date type. When you read it back out (in JavaScript), you'll get a Date object back, and you can get the unix epoch-based time from that.

本文标签: javascripthow to write server timestamp in milliseconds to FirestoreStack Overflow