Home > Software design >  Get Epoch timestamp accurate by the day with datetime
Get Epoch timestamp accurate by the day with datetime

Time:03-07

I want to get a day-accurate (not hour, minutes, seconds) Epoch timestamp that remains the same throughout the day.

This is accurate by the millisecond (and therefore too accurate):

from datetime import date, datetime
timestamp = datetime.today().strftime("%s")

Is there any simple way to make it less precise?

CodePudding user response:

take Unix time as integer seconds since the epoch and floor to multiples of seconds in a day:

from datetime import datetime, timezone

# current unix time in seconds
t = int(datetime.now(timezone.utc).timestamp())

# floor to multiples of seconds in a day:
unixtime_day = t - (t           
  • Related