【问题标题】: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()
    

    如果有人感到困惑,我可以进一步解释。祝大家编码愉快!

    【讨论】:

      猜你喜欢
      • 2018-09-26
      • 2017-02-15
      • 1970-01-01
      • 2011-12-26
      • 2011-07-02
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多