【问题标题】:how to convert firebase time stamp to date angular如何将firebase时间戳转换为日期角度
【发布时间】:2022-09-30 23:05:02
【问题描述】:

打字稿

    public Patients: Observable<any>;
    constructor(private db: AngularFirestore) {
        this.Patients = this.db.collection(\'Patients\').valueChanges();

// get data from firebase :

     ngOnInit(): void {
    this.Patients.subscribe((response) => {
      this.prepareData(response);
      this.console.log(response);
    });
  }

  

// in this section use for add data to apexcharts

      prepareData(patients) {
    
     patients.forEach((patient) => {
    
      });
      }
  • “firebase 时间戳”是怎样的?您可以在问题中包含这样的字符串吗?
  • firebase 时间戳 // Object { seconds: 1612890000, nanoseconds: 0 } 我想改成这个 2019-07-07

标签: angular


【解决方案1】:

我猜 Firebase 时间戳在 ISO 8601 中,并且 JavaScript Date 方法不完全支持。

要将其转换为进一步使用,您可以尝试以下操作:

const firebaseDate = "20190412T131518.000Z";
const convertedDate = new Date(firebaseDate.replace(/(....)(..)(.....)(..)(.*)/, '$1-$2-$3:$4:$5'));
console.log(convertedDate);

编辑刚刚看到你的评论回复你可以简单地将秒传递给日期方法

const firebaseObj = { seconds: 1612890000, nanoseconds: 0 }
const convertedDate = new Date(firebaseObj.seconds)

console.log(convertedDate)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 2018-12-09
    • 2020-11-17
    • 2019-08-24
    • 2012-04-10
    • 2016-11-26
    相关资源
    最近更新 更多