Firebase new Date()?

后端 未结 2 465
广开言路
广开言路 2020-12-14 18:42

I\'m new to Firebase and I was wondering how I can store a JavaScript date and use Firebase to compare them on the client side later?

I want to do something like:

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 19:02

    You can also use timestamp.

    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
        }
    
    });
    

提交回复
热议问题