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 compare 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?
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With