【问题标题】:How to get current System Timestamp for Firebase? in Android Kotlin如何获取 Firebase 的当前系统时间戳?在 Android Kotlin 中
【发布时间】:2020-11-20 00:54:12
【问题描述】:
我正在使用 Firebase 分页,因此我需要在 firebase 中查询数据库的当前时间戳?谁能知道如何从系统获取当前时间戳?
FirebaseFirestore.getInstance().collection("news")
.orderBy("timestamp",Query.Direction.DESCENDING)
.startAfter(timestamp) // for here i need current system timestamp..?? how can i??
.limit(4)
.get()
【问题讨论】:
标签:
firebase
kotlin
google-cloud-firestore
【解决方案1】:
firebase.firestore.FieldValue.serverTimestamp()
如果您想要firebase.firestore.FieldValue.serverTimestamp() 的日期值,您可以使用.toDate()。
见FireStore Timesteamp。
如果您希望当前日期作为时间戳,您可以使用以下内容
对于 Firebase 函数
import admin = require('firebase-admin');
const todayAsTimestamp = admin.firestore.Timestamp.now()
对于本地项目
import { Timestamp } from '@google-cloud/firestore';
const myTimestampAsDate = Timestamp.now()
【解决方案2】:
Ferin 的回答让我朝着正确的方向前进。在我当前使用 Kotlin 的项目中,当我将时间戳值存储在 Firebase Cloud Firestore 中时,这对我有用。
val createdAt = FieldValue.serverTimestamp()
如果有人感到困惑,我可以进一步解释。祝大家编码愉快!