18

我是 Firebase 的新手,我想知道如何存储 JavaScript 日期并稍后使用 Firebase 在客户端进行比较?

我想做类似的事情:

var someDate = new Date();
myRootRef.set(someDate);

myRootRef.on('value', function(snapshot) {
     var currDate = new Date();
if (currDate >=  snapshot.val()){
     //do something
}

null但是,我从快照中取回了一个值?

4

2 回答 2

22

您也可以使用时间戳。

var timestamp = new Date().getTime();
myRootRef.set(timestamp);

myRootRef.on('value', function(snapshot) {
    var currDate = new Date();
    var snapshotTimestamp = snapshot.val();

    //you can operate on timestamps only...
    console.log("Snapshot timestamp: " + snapshotTimestamp);

    if(currDate.getTime() >= snapshotTimestamp) {
        //do something
    }

    //...or easily create date object with it  
    console.log("Snapshot full date: " + new Date(snapshotTimestamp));

    if (currDate >=  new Date(snapshotTimestamp)){
        //do something
    }

});
于 2013-05-05T09:56:14.370 回答
-3

您需要使用字符串或带有值的对象设置 Firebase,然后在事件中读取相同的值。

    var someDate = new Date();
    myRootRef.set({Date: someDate.toString()});

    myRootRef.on('value', function(snapshot) {
    var msg = snapshot.val();
     var currDate = new Date();
    if (currDate >=  msg.Date){
     //do something
    }
    });
于 2013-05-05T09:51:26.213 回答