简体   繁体   English

如何将 object 时间戳转换为 firebase 时间戳?

[英]How to convert object timestamp to firebase timestamp?

I want to convert the following timestamp:我想转换以下时间戳:

Object {
 "_nanoseconds": 725000000,
 "_seconds": 1621386976,
}

to this timestamp:到这个时间戳:

t {
  "nanoseconds": 725000000,
  "seconds": 1621386976,
}

How can I go about doing this?我怎么能 go 关于这样做? I am stumped.我难住了。 I have tried toDate() and variations of this, but nothing is working.我已经尝试过toDate()和它的变体,但没有任何效果。

Found this on reddit:在reddit上找到这个:

getTimeText = (timeObject: any) => {
    // Convert to time text once it's of type firestore.Timestamp
    const getTextFromTimestamp = (timestamp: app.firestore.Timestamp) => {
        return this.timeAgo.format(timestamp.toDate())

    }
    if (timeObject instanceof app.firestore.Timestamp) {
        // Check if Timestamp (accessed from client SDK)
        return getTextFromTimestamp(timeObject)
    } else if (Object.prototype.toString.call(timeObject) === '[object Object]') {
        // Check if it's a Map (accessed from Cloud Functions)
        const seconds = timeObject['_seconds']
        const nanoseconds = timeObject['_nanoseconds']
        if (seconds && nanoseconds) {
            const timestamp = new app.firestore.Timestamp(seconds, nanoseconds)
            return getTextFromTimestamp(timestamp)
        }
    }
    console.log('Couldn\'t parse time', timeObject)
    // Fallback
    return 'some time ago'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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