[英]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.我从 firebase 得到这个对象,我想得到这种格式: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并在 html
{{seconds | firebaseDate}}
dont forget to add to app.modules
declarations
this pipe不要忘记将此管道添加到
app.modules
declarations
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.