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 badges1 Answer
Reset to default 8This 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)))