最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase Database Rule that compares timestamp to 00:00:00 of today? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to create a Firebase Database Rule that forbids users to write to any node which has a timestamp created before 00:00:00 of today.

Example of database:

{
    "messageID": { 
        "message": "blabla",
        "timestamp": 1481744636
    }
}

Rules:

{
    "rules": {
        "$messageID": {
            ".write": "data.child('timestamp').val() > ???"
        }
    }
}

I know about the 'now' keyword, but I don't think it helps me. I need a way to pare the timestamp with a value for 00:00:00 of today.

I could create and store a table with all the unix epoch times for midnight of each day in the futurem but isn't there a way to calculate it on the fly?

I'm trying to create a Firebase Database Rule that forbids users to write to any node which has a timestamp created before 00:00:00 of today.

Example of database:

{
    "messageID": { 
        "message": "blabla",
        "timestamp": 1481744636
    }
}

Rules:

{
    "rules": {
        "$messageID": {
            ".write": "data.child('timestamp').val() > ???"
        }
    }
}

I know about the 'now' keyword, but I don't think it helps me. I need a way to pare the timestamp with a value for 00:00:00 of today.

I could create and store a table with all the unix epoch times for midnight of each day in the futurem but isn't there a way to calculate it on the fly?

Share Improve this question edited Dec 15, 2016 at 2:12 AL. 37.8k10 gold badges147 silver badges285 bronze badges asked Dec 14, 2016 at 20:38 Dan FlictDan Flict 19911 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

This should do it:

{
    "rules": {
        "$messageID": {
            ".write": "data.child('timestamp').val() > (now - (now % 86400000))"
        }
    }
}

86,400,000 is the number of milliseconds in 24 hours, so now % 86400000 should give you the number of milliseconds since 00:00:00 UTC.

console.log(new Date(Date.now() - (Date.now() % 86400000)))

发布评论

评论列表(0)

  1. 暂无评论