【问题标题】:How to print Firestore timestamp as formatted date and time like December 28,2020 at 3:52:04 AM UTC+5:30?如何将 Firestore 时间戳打印为格式化的日期和时间,例如 2020 年 12 月 28 日凌晨 3:52:04 UTC+5:30?
【发布时间】:2021-04-06 01:37:17
【问题描述】:

从 Firestore 获取日期时,我得到时间戳“秒:1608490949,纳秒:275000000”。我想将其打印为格式正确的日期和时间。因为它是“2020 年 12 月 28 日凌晨 3:52:04 UTC+5:30”。 下面是我的代码图片

    obj.modify=this.dateconversion(obj.modify);
    dateconversion( time:Timestamp){
      return time.toDate();
    }
    
    It is returning  me values "2020-12-27T22:22:04.000Z" but actual in firestore "December 28,2020 at 
    3:52:04 AM UTC+5:30".
    Its seems giving me 5 hours back that's why one day back data is printing.
    Can any one please suggest other way to do or where I am doing mistake.

【问题讨论】:

  • Firestore 时间戳类型以 UTC 格式存储日期,当您在 Google 控制台上看到时,它会为您提供本地日期时间的转换。所以告诉我,当你说它返回时是什么意思。?您使用哪种语言与 Firestore 进行通信?
  • 那么,我将如何转换为早先“2020 年 12 月 28 日凌晨 3:52:04 UTC+5:30”中的格式?当我将日期格式(时间戳{秒“123”nanoseond = 12345)转换为日期时。我正在使用nestjs

标签: javascript google-cloud-firestore nestjs


【解决方案1】:

您可以在此处调用 javascript 日期函数,因为这会转换为 javascript 日期

dateconversion( time:Timestamp){
  return time.toDate().toString();
  
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString

或者为了更多地控制格式,你可以使用momentjs [https://momentjs.com/docs/]

【讨论】:

    猜你喜欢
    • 2019-10-30
    • 2020-12-26
    • 2014-04-26
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多