简体   繁体   中英

How can I Get YYYY/MM/DD of Timestamp firebase with AngularFire

I get this object from firebase and I would like to get this format: 2020-02-05.

t {seconds: 1601656203, nanoseconds: 529000000}
nanoseconds: 529000000
seconds: 1601656203
__proto__: Object

This is my code:

  public getAllUsers(): Observable<Users[]> {
    return this.afs.collection<Users>('Users', ref => ref.orderBy('createdAt'))
      .snapshotChanges()
      .pipe(
        map(actions =>
          actions.map(a => {
            const data = a.payload.doc.data() as Users;
            const docid = a.payload.doc.id;
            return { docid, ...data };
          })
        )
      );
  }

 function toDateTime(secs) { var t = new Date(1970, 0, 1); t.setSeconds(secs); let year=t.getFullYear(); let month=t.getMonth(); let day=t.getDate() return year+"-"+less10(month)+"-"+less10(day); } function less10(time){ return time<10 ? "0"+time :time; } console.log(toDateTime(1601656203))

if you dont use date inside any function then I suggest you to use custom pipe for this.

  import { Pipe, PipeTransform } from '@angular/core';
   @Pipe({
      name: 'firebaseDate',
      pure: false
   })
    
   export class FireBaseTimePipe implements PipeTransform {
      transform(value: number): any {  
        var t = new Date(1970, 0, 1);       
        t.setSeconds(value);
        let year=t.getFullYear();
        let month=t.getMonth();
        let day=t.getDate()
        return year+"-"+this.less10(month)+"-"+this.less10(day);        
      }
      less10(time){
          return time<10 ? "0"+time :time;
      }
    }

and in html

{{seconds |  firebaseDate}}

dont forget to add to app.modules declarations this pipe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM