【问题标题】:Google functions firestore dateGoogle 功能 Firestore 日期
【发布时间】:2020-01-14 04:37:27
【问题描述】:

我正在尝试将数据从函数存储到 Firestore,但日期有问题。 我的功能:

const newScheduleEventRef = db.collection('scheduleEvents').doc();  
batch.set(newScheduleEventRef, {
  user_id: context.auth.uid,  
  category: 1,
  startDate1: new Date(year, month, day, 0, 0, 0),  
  startDate2: new Date(year, month, day, 0, 0, 0),
  endDate1: new Date(year, month, day, 23, 59, 59),
  endDate2: new Date(year, month,day, 23, 59, 59),
  eventAlias: entry,
  addDate: new Date()
});

这是我得到的:

这可能是由于TimeZone,但我不知道如何解决它。

谢谢。

【问题讨论】:

    标签: javascript date google-cloud-firestore google-cloud-functions


    【解决方案1】:

    Firestore 时间戳字段实际上并不在其中存储时区。事实上,JavaScript Date 对象也没有。当您编写时间戳字段时,您始终以 UTC 为基础。根据API documentation for Timestamp

    时间戳表示独立于任何时区的时间点或 日历,表示为秒和秒的分数 UTC 纪元时间的纳秒分辨率。

    您看到的是 Firestore 控制台使用本地计算机上配置的时区呈现这些日期。如果你让世界各地的人看同一件事,他们会看到相同的时间点,但时区不同。

    如果您想在特定时区编写带有特定时钟时间的时间戳,您应该使用一些日期操作库,例如 MomentJS,以确保您在您想要的时区拥有正确的时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多