简体   繁体   中英

find day and time from the mentioned total seconds starting from beginning of the week - python

Below is the total seconds starting from beginning of the week. FYI - 86400 seconds in a day.

590400

I want to write some logic and display like SUNDAY 08:00PM using python

It's not clear what week you want to start on. Either pick one fixed "Monday" somewhere and go from there (as demonstrated below), or calculate this week's particular Monday, or whatever your logic is. This is important should you be dealing with timezones with DST, since not every day is 86400 seconds long.

from datetime import datetime, timedelta

monday = datetime(2019, 9, 23)
ts = monday + timedelta(seconds=590400)
print(ts.strftime('%A %I:%M %p'))  # Sunday 08:00 PM
from datetime import datetime
t = 590400
h = 590400%86400

#calculating week
y = int(590400/86400)
if y == 6:
    print("Saturday")

res = str(int(h/3600)) + ':00'
d = datetime.strptime(res, "%H:%M")
print(d.strftime("%I:%M %p"))

Saturday 08:00 PM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM