Javascript 如何用当前时间减去firebase时间戳,并检查其角度是否超过5分钟
我有一个angular应用程序,其中我使用Javascript 如何用当前时间减去firebase时间戳,并检查其角度是否超过5分钟,javascript,angular,momentjs,angular2-moment,Javascript,Angular,Momentjs,Angular2 Moment,我有一个angular应用程序,其中我使用firebase.database.ServerValue.timestamp 现在我想检查添加到数据库的时间戳是否超过五分钟 我试着在我的组件中使用矩,但它不起作用。它提供的是虚假数据。我猜Firebase中存储的数据正处于一个新纪元,就像我在印度一样,所以如何使力矩以角度工作 我用的是角动量 这就是我试过的 import * as moment from 'moment/moment'; edit(i:number){
firebase.database.ServerValue.timestamp
现在我想检查添加到数据库的时间戳是否超过五分钟
我试着在我的组件中使用矩,但它不起作用。它提供的是虚假数据。我猜Firebase中存储的数据正处于一个新纪元,就像我在印度一样,所以如何使力矩以角度工作
我用的是角动量
这就是我试过的
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)代码>=星期三8月5日49474 20:09:20 GMT+0530(印度标准时间)
现在的情况是——2017年7月4日星期二14:56:46 GMT+0530(印度标准时间)
请帮助解释为什么注释数据不在7月3日的时间内下沉,因为它是在firebase上添加的您可以使用javascript日期对象进行计算,即
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);
this.comments[0].createdAt的值是多少?它是存储在FireBase中的纪元时间,单位为毫秒(即javascript类型)或者秒数?秒数显示正确的信息消息是在最后一天存储的,但仍然在控制台中。日志显示为false,因为超过分钟,所以当我控制台记录消息时,应显示为true right-周三8月5日49474 20:09:20 GMT+0530(印度标准时间)和现在-2017年7月4日星期二14:56:46 GMT+0530(印度标准时间)是,因为当您记录一个日期对象时,它会将toString()应用到它本身,这是一个人类可读的字符串,这就是为什么您在diff中使用.getTime(),以便它将日期转换为毫秒
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