How to convert a human date to EPOCH range that covers the entire day?

Question:

I am trying to convert a human-readable date to an EPOCH range, where the range covers that entire day.

I couldn’t find any existing function that does it- anyone know if there is one?

Example:
So if we converted today’s date (15/09/22) into EPOCH at midnight start of the day, I believe it would be 1663200000. However from 1663200000 to 1663286399 it is still today- the latter being midnight today.

Asked By: Patrick_Chong

||

Answers:

You can just use the python datetime lib.

>>> import datetime as dt
>>> (dt.datetime(2022, 9, 15).timestamp(), dt.datetime(2022, 9, 16).timestamp()-1)
(1663171200.0, 1663257599.0)

# add tzinfo
>>> (dt.datetime(2022, 9, 15, tzinfo=dt.timezone.utc).timestamp(), dt.datetime(2022, 9, 16, tzinfo=dt.timezone.utc).timestamp()-1)
(1663200000.0, 1663286399.0)
Answered By: yunzhi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.