Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Firestore中将时间戳转换为可读日期_Javascript_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript 在Firestore中将时间戳转换为可读日期

Javascript 在Firestore中将时间戳转换为可读日期,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我正在从集合中提取数据。我以时间戳Firestore标准格式保存了日期。这就是我保存记录['entryDate']=firebase.firestore.FieldValue.serverTimestamp()的方式 现在,当我获取它时,我希望它在数组中的一个新变量中是可读的格式。这是我当前用于获取数据的代码: home.ts ngOnInit { //start fetch this.crudService.read_Projects().subscribe(d

我正在从
集合
中提取数据。我以
时间戳
Firestore标准格式保存了日期。这就是我保存
记录['entryDate']=firebase.firestore.FieldValue.serverTimestamp()的方式

现在,当我获取它时,我希望它在数组中的一个新变量中是可读的格式。这是我当前用于获取数据的代码:

home.ts

ngOnInit {
        //start fetch
        this.crudService.read_Projects().subscribe(data => {
          this.projectList = data.map(e => {
            return {
              id: e.payload.doc.id,
              isEdit: false,
              name: e.payload.doc.data()['name'],
              desc: e.payload.doc.data()['desc'],
              followers: e.payload.doc.data()['followers'],
              milestone: e.payload.doc.data()['milestone'],
              entryDate: e.payload.doc.data()['entryDate'],

             // for each readable_entryDate =  how do i convert ?

            };
          })
          console.log(this.projectList);
        });//end fetch
}
crud.service.ts

  read_Projects() {
    return this.firestore.collection('projects').snapshotChanges();
  }

我可以使用
get()
执行此操作,但在上面的代码中无法执行此操作,以防您希望看到我的
get()
获取日期并将其转换为可读格式的代码:

 const ref = firebase.firestore().collection('cities').where('name', '==', 'JALANDHAR')
 .get()
 .then(function(querySnapshot) {
   querySnapshot.forEach(function(doc) {
   const timeinmillsecs = doc.data().timestamp.seconds * 1000; //1. get timestamp and make it in milli
   console.log(new Date(timeinmillsecs)); // 2. convert to time lambi string
   let a1 = new Date(timeinmillsecs); // 3. convert to simple date
   let show_year_month_date =  a1.getFullYear()+'/'+(a1.getMonth()+1) +'/'+a1.getDate(); // 3.convert to imple date
   console.log(show_year_month_date); // 4. needed result // will show this on form
 });
 })
 .catch(function(error) {
     console.log("Error getting documents: ", error);
 });
编辑1 使用
ToDate()
后,将我的代码更改为


entryDate:e.payload.doc.data()['entryDate'].toDate(),

我得到的是完整的JS date对象,但我只需要日期,可能在数组中的
entryDate2
字段中,我如何实现这一点

以下是现在的日期:

这不是必需的:

doc.data().timestamp.seconds * 1000
您只需调用Timestamp对象即可获得JavaScript日期


在此之后,您可能希望研究如何使用库,例如根据用户的区域设置和首选项格式化日期。

如何在此处使用toDate()
entryDate:e.payload.doc.data()['entryDate']
entryDate:e.payload.doc.data()['entryDate'].toDate(),
给我js对象,我只需要日期,现在我该如何按照我的代码进行……有点困惑,我想要
entryDate2
在我的数组中只包含日期。正如我在回答中所说,你可以研究使用momentjs来格式化日期。