如何将Firestore日期/时间戳转换为JS Date()?

blu*_*her 25 javascript firebase google-cloud-firestore

我想将以下日期转换为javascript Date()对象.当我从服务器返回时,它是一个Timestamp对象,

Firebase Firestore控制台的屏幕截图:

在此输入图像描述

当我在firestore返回的对象列表上尝试以下操作时:

  list.forEach(a => {
    var d = a.record.dateCreated;
    console.log(d, new Date(d), Date(d))
  })
Run Code Online (Sandbox Code Playgroud)

我得到这个输出: 在此输入图像描述

显然,时间戳都是不同的,并不是2018年9月9日(恰好是今天)的相同日期.我也不确定为什么会new Date(Timestamp)导致invalid date.我是一个JS新手,我对日期或时间戳做错了吗?

Dou*_*son 57

JavaScript Date的构造函数对Firestore Timestamp对象一无所知- 它不知道如何处理它们.

如果要将时间戳转换为日期,请在时间戳上使用toDate()方法.

  • @blueether 或其他人可以发布一个工作示例吗?因为我只收到错误“...toDate() is not a function”。 (33认同)
  • toDate() 不是一个函数 (6认同)
  • 任何遇到“toDate() is not a function”错误并使用 JavaScript SDK 的人:对我来说,我可以使用“new Date(timestamp)”将时间戳直接转换为日期。 (5认同)
  • 对于任何遇到“toDate() is not a function”错误的人,可能是因为您在 JSON 对象上使用它。您可以简单地使用从 firebase 收到的时间戳对象,或者如果您无法直接使用它,那么要将其转换回时间戳对象,您可以执行以下操作:`const timestamp = new firebase.firestore.Timestamp(jsonTimestamp.seconds, jsonTimestamp) .nanoseconds)` 现在执行 `timestamp.toDate()` 将会起作用 (4认同)
  • 只是想了解一下。为什么OP应该发布一个新问题而不是更新他当前的问题? (2认同)

Wal*_*riq 31

除了其他答案之外,你也可以这样做

    //date from firebase is represented as
    let time = {
      seconds: 1613748319,
      nanoseconds: 47688698687,
    }
    
    const fireBaseTime = new Date(
      time.seconds * 1000 + time.nanoseconds / 1000000,
    );
    const date = fireBaseTime.toDateString();
    const atTime = fireBaseTime.toLocaleTimeString();


    console.log(date, atTime);
Run Code Online (Sandbox Code Playgroud)


小智 30

您可以使用 toDate() 函数和 toDateString() 来单独显示日期部分。

const date = dateCreated.toDate().toDateString()
//Example: Friday Nov 27 2017
Run Code Online (Sandbox Code Playgroud)

假设您只想要时间部分,然后使用 toLocaleTimeString()

const time = dateCreated.toDate().toLocaleTimeString('en-US')
//Example: 01:10:18 AM, the locale part 'en-US' is optional
Run Code Online (Sandbox Code Playgroud)

  • 这个答案就是我一直在寻找的。对于任何想要将相应时间(在我的例子中是中部时间)转换为东部/纽约时间的人来说,还有一个额外的提示,您可以执行以下操作:`const time = dateCreated.toDate().toLocaleTimeString('en-US' , { 时区: '美国/纽约' })` (2认同)
  • 这个答案应该是公认的答案。 (2认同)

小智 22

请使用 toDate() 方法,然后使用像这样的角管将其转换为格式 -

{{ row.orderDate.toDate() | 日期:'dd MMM hh:mm' }}

  • 这对我不起作用,它失败了。有角?谁说首先使用 Angular?! (4认同)

Waj*_*ath 19

您可以使用Timestamp.fromDate.toDate用于来回转换。

// Date to Timestamp
const t = firebase.firestore.Timestamp.fromDate(new Date());

// Timestamp to Date
const d = t.toDate();
Run Code Online (Sandbox Code Playgroud)


Dil*_*age 16

如何将 Unix 时间戳转换为 JavaScript 日期对象。

var myDate = a.record.dateCreated;
new Date(myDate._seconds * 1000); // access the '_seconds' attribute within the timestamp object
Run Code Online (Sandbox Code Playgroud)


Nag*_*aba 13

终于,我得到了我需要的东西。这将返回日期为08/04/2020

new Date(firebase.firestore.Timestamp.now().seconds*1000).toLocaleDateString()
Run Code Online (Sandbox Code Playgroud)


小智 9

一种简单的方法是使用toMillis()firestore 时间戳上的方法将 firestore 时间戳转换为纪元时间戳。
例如:您有一个 firestore 时间戳
created_on : Timestamp { _seconds: 1622885490, _nanoseconds: 374000000 }

let epochTimestamp = created_on.toMillis()
//epochTimestamp = 1621081015081
//Now this timestamp can be used as usual javascript timestamp which is easy to manipulate.
let date = new Date(epochTimestamp) //date = Sat May 15 2021 17:46:55 GMT+0530 (India Standard Time)
Run Code Online (Sandbox Code Playgroud)


小智 6

从 firestore 获取的时间戳对象有一个toDate()可以使用的方法。

list.forEach(a => {
  var d = a.record.dateCreated;
  console.log(d.toDate())
})
Run Code Online (Sandbox Code Playgroud)

toDate()这是 firebase 文档中有关该方法的引用

将时间戳转换为 JavaScript 日期对象。此转换会导致精度损失,因为 Date 对象仅支持毫秒精度。

返回表示与此 Timestamp 相同的时间点的 Date JavaScript Date 对象,精度为毫秒。

https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp#todate


Pro*_*tay 6

这可能有帮助:

new Date(firebaseDate._seconds * 1000).toUTCString()
Run Code Online (Sandbox Code Playgroud)


小智 5

const timeStampDate = record.createdAt;
const dateInMillis  = timeStampDate._seconds * 1000

var date = new Date(dateInMillis).toDateString() + ' at ' + new Date(dateInMillis).toLocaleTimeString()
Run Code Online (Sandbox Code Playgroud)

输出示例: Sat 11 Jul 2020 at 21:21:10