I have a timestamp
timestamp = Time.now.utc.to_f
I need to find the timestamp at midnight UTC the same day:
t = Time.at timestamp
Time.at(((t - t.hour * 3600).to_f / 3600).to_i * 3600).to_f
is there a way to get the same timestamp without having to convert the original timestamp to Time object? In a way similar to getting a timestamp rounded to the most recent hour:
(timestamp / 3600).to_i * 3600
Since the UTC timestamp starts on January 1, 1970 at midnight UTC, just do:
timestamp - timestamp % (3600*24)
That will give you the timestamp at the last midnight UTC.
To adjust for your time zone, just add or remove the corresponding number of hours before calculating the modulo and then add or remove them back to get the corresponding UTC timestamp.
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