如何计算两次的不同?一个在firebase中,一个是当前时间

问题描述 投票:1回答:1

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

//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

javascript firebase firebase-realtime-database
1个回答
1
投票

使用Date.now()将时间存储为UTC时间戳。只需从较大的较小值中减去较小值即可。将用户可读的时间格式化为UI用途。

https://rack.pub/timestamp

© www.soinside.com 2019 - 2024. All rights reserved.