我有一个 Angular 应用程序,其中我使用 firebase.database.ServerValue.TIMESTAMP
将带有时间戳的记录存储到 firebase
现在我想检查添加到数据库的时间戳是否超过五分钟。
我尝试在我的组件中使用 moment,但它不起作用。它提供了虚假数据。我猜想 Firebase 中存储的数据所处的时代就像我在印度一样,那么如何让 moment 在 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.comments 时的值
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 上添加的
最佳答案
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 - 如何在 Angular 中用当前时间减去 firebase 时间戳并检查它是否超过 5 分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44892322/