如何在 Angular 中用当前时间减去 firebase 时间戳并检查它是否超过 5 分钟



我有一个角度应用程序,在其中我使用firebase.database.ServerValue.TIMESTAMP将带有时间戳的记录存储到 Firebase

现在我想检查添加到数据库的时间戳是否超过五分钟。

我尝试在我的组件中使用时刻,但它只是工作.它正在提供虚假数据.我想存储在Firebase中的数据是在时代,因为我在印度,所以如何在Angular中使时刻工作。

我正在使用 Angular 2 Moment。

这是我尝试过的

import * as moment from 'moment/moment';
edit(i:number){
const val = moment.unix(this.comments[0].createdAt);// the value is a epoch time that is the standard that is stored in firebase
const present = moment().unix();
console.log(moment.duration(val.diff(present)));
}

在日期中使用时的值。注释

let ts = new Date(this.comments[0].createdAt * 1000);= 周三 8 月 5 日 49474 20:09:20 GMT+0530(印度标准时间(

至于现在它喜欢的地方 -- 周二 Jul 04 2017 14:56:46 GMT+0530 (印度标准时间(

请帮助为什么它是评论数据不是随着时间应该是 7 月 3 日而下沉,因为它是当天在 Firebase 上添加

const now = new Date();
const ts = new Date(this.comments[0].createdAt);
const diff = now.getTime() -  ts.getTime();
console.log(diff > 5 * 60 * 1000); // this will give you true or false

您可以使用javascript Date对象进行数学运算,即

let now = new Date();
let ts = new Date(this.comments[0].createdAt * 1000);
let diff = now.getTime() - ts.getTime();
console.log(diff > 5 * 60 * 1000);

相关内容

最新更新