简体   繁体   中英

Cloud Firestore How to subtract hour from Timestamp

I am able to get the timestamp using this line in my index.js.

var now = admin.firestore.Timestamp.now();

I just want the timestamp that's 1 hour before now.

I've tried this and hasn't worked.

var old = now(now.seconds-1*60*60,milliseconds);

Also tried but it returns just the seconds instead of a timestamp

var old = admin.firestore.Timestamp.now().seconds - 1*60*60;

Any ideas on how to get the timestamp of an hour before?

Firestore's Timestamp class doesn't offer the ability to do "date math". You should do any date math using a native date type for your language, then convert that to a Timestamp when you're done.

Since you tagged this JavaScript, you could use a normal Date object to do the math:

const date = new Date(Date.now() - 60*60*1000)

The convert that to a Timestamp with Timestamp.fromDate() :

const timestamp = admin.firestore.Timestamp.fromDate(date)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM