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

时间:2017-07-03 18:56:55

标签: javascript angular momentjs angular2-moment

我有一个角度应用程序,我使用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)));
      }

在日期使用时的this.com的价值

let ts = new Date(this.comments[0].createdAt * 1000); = Wed Aug 05 49474 20:09:20 GMT + 0530(印度标准时间)

现在就像它一样 - 2017年7月4日星期二14:56:46 GMT + 0530(印度标准时间)

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

2 个答案:

答案 0 :(得分:0)

您可以使用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);

答案 1 :(得分:0)

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