我正在一个允许用户发布内容的网站上,我想添加一个“趋势”部分。自发布帖子以来,我将如何计算经过的秒数?
我为每个帖子存储了一个Firebase服务器时间戳:
topic_obj.time = firebase.database.ServerValue.TIMESTAMP ;
任何帮助将不胜感激!
答案 0 :(得分:1)
您可以使用moment.js来计算Javascript中的时差:
firebase.database.ServerValue.TIMESTAMP将是自Unix时代以来的毫秒数,请参见Firebase docs
let timeStamp = firebase.database.ServerValue.TIMESTAMP;
let endTime = moment();
let startTime = moment(timeStamp);
var duration = moment.duration(endTime.diff(startTime));
var durationSeconds = duration.asSeconds();
console.log('Duration (seconds): ', durationSeconds);
一次也可以减去:
let timeStamp = firebase.database.ServerValue.TIMESTAMP;
let now = new Date().getTime();
let differenceSeconds = (now - timeStamp)/1000;