Firebase new Date()? [英] Firebase new Date()?

查看:107
本文介绍了Firebase new Date()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我想请执行以下操作:

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

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

但是,我收回一个

推荐答案

您还可以使用时间戳。 c> / p>

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
    }

});

这篇关于Firebase new Date()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆