Cloud Functions new timestamp requirements

531 views
Skip to first unread message

shaun larkin

unread,
Jan 17, 2019, 10:53:27 PM1/17/19
to Firebase Google Group
Can some please EL5 what I need to do to get the current date in a cloud function? I know that Date.now() is no longer viable (you get a warning now saying your app may stop working). I can't seem to wrap my head around this:

old 
var now = Date.now()

new
const now = admin.firestore().settings({timestampsInSnapshots : true})

even the cloud function github examples use the JS date object: 


I essentially want to flag all documents that have an date["endDate"] key that is on or before the current date.

thanks in advance!.

Samuel Stern

unread,
Jan 18, 2019, 12:32:36 PM1/18/19
to fireba...@googlegroups.com
Hey Shaun,

I think you've confused a few things here, so let me back up and explain.  Cloud Firestore has support for fields that contain a Date/Time.  When the product first came out in Beta this meant that we recommended you use the native Date object on your platform of choice.  However there are differences in how each platform (Android, iOS, Web, etc) handles Dates so we decided to make a new Firestore-specific "Timestamp" type and encourage developers to use that whenever possible to make sure we get cross-platform and round-trip consistency.

So where you previously had:
var now = Date.now();

You now want to use:
var now = admin.firestore.Timestamp.fromDate(Date.now())

Note that none of this is specific to Cloud Functions.  This is a change in Cloud Firestore.  You have mentioned this setting:
admin.firestore().settings({timestampsInSnapshots : true})

This means that even if you do put a Date() object into Firestore, when you read that field back you will get a Firestore Timestamp type.  This will be the default in the future so it's best to enable it now and learn to work with it.

- Sam

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/5f12f55e-0a5f-4f30-8c6a-10c3b87ba077%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

shaun larkin

unread,
Jan 18, 2019, 9:43:53 PM1/18/19
to Firebase Google Group
Very clear! Thanks so much Samuel!
Reply all
Reply to author
Forward
0 new messages