javascript - 如何计算两个时间的差异?一个在 firebase 中,一个是当前时间

标签 javascript firebase firebase-realtime-database

我想计算时间之间的差异,但我不知道该怎么做。首先我得到电流,然后我从数据库中检索时间。我不知道如何创建一个方程式来计算它们。

//get the time
h = now.getHours();
m = now.getMinutes();
if (h < 10)
    h = '0' + h;
if (m < 10)
    m = '0' + m;
var timestring = h + ":" + m;


var query = firebaseRef.orderByChild('carplate').equalTo(carplate);

query.once('value', function(snapshot) {
    snapshot.forEach(function(child) {
        var time = child.val().time;

        var diff = timestring - time; //how to calculate the different

    })
});

My Database

最佳答案

使用 Date.now() 将时间存储为 UTC 时间戳。只需用较大的值减去较小的值即可得到差值。仅出于 UI 目的将时间格式化为人类可读。

https://rack.pub/timestamp

关于javascript - 如何计算两个时间的差异?一个在 firebase 中,一个是当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49114222/

相关文章:

javascript - Appcelerator Javascript 搜索功能

javascript - 如何在javascript中的2个模式之间拆分字符串

javascript - 如何从文本框中获取姓名和电子邮件ID?

firebase - Flutter Firebase authStateChanges() 且 Provider 和 NullSafety 打开

javascript - Firebase 云函数 onCall 在更改区域后不起作用

javascript - 在使用 Firebase 的 React-native GiftedChat 聊天应用程序中加载早期消息时遇到问题

javascript - 使用 JavaScript 通过悬停应用样式不适用于类

swift - firestore 中的 "DatabaseHandle"是什么?

JavaScript 代码不改变动态生成的 html 代码的样式

javascript - 错误类型错误 : Cannot set property 'product' of null