Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase new Date()?

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:

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

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

However, I get back a value of null from the snapshot?

like image 682
TriFu Avatar asked May 05 '13 09:05

TriFu


1 Answers

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
    }

});
like image 61
ghost Avatar answered Oct 08 '22 04:10

ghost