简体   繁体   English

从一周开始的上述总秒数中查找日期和时间 - python

[英]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.仅供参考 - 一天 86400 秒。

590400

I want to write some logic and display like SUNDAY 08:00PM using python我想使用 python 编写一些逻辑和显示,例如 SUNDAY 08:00PM

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.要么在某个地方选择一个固定的“星期一”,然后从那里选择 go(如下所示),要么计算本周的特定星期一,或者任何你的逻辑。 This is important should you be dealing with timezones with DST, since not every day is 86400 seconds long.如果您使用 DST 处理时区,这一点很重要,因为并非每天都是 86400 秒长。

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周六 08:00 PM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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