[英]How can I Get YYYY/MM/DD of Timestamp firebase with AngularFire
我從 firebase 得到這個對象,我想得到這種格式:2020-02-05。
t {seconds: 1601656203, nanoseconds: 529000000}
nanoseconds: 529000000
seconds: 1601656203
__proto__: Object
這是我的代碼:
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))
如果您不在任何函數中使用日期,那么我建議您為此使用自定義管道。
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;
}
}
並在 html
{{seconds | firebaseDate}}
不要忘記將此管道添加到app.modules
declarations
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.