from datetime import datetime, timedelta
# 當(dāng)前日期和時間
current_datetime = datetime(year=2024, month=5, day=7, hour=19, minute=21, second=23)
# 北京所在的時區(qū)
beijing_timezone = datetime.timedelta(hours=+8)
# 計算從昨天的同一時間開始到現(xiàn)在的時間差
delta = current_datetime - datetime(year=current_datetime.year, month=current_datetime.month, day=current_datetime.day, hour=current_datetime.hour, minute=current_datetime.minute, second=current_datetime.second)
# 如果昨天的時間還沒過23:59,則直接返回昨天的時間
if delta.total_seconds() < 86400:
yesterday_datetime = current_datetime.replace(hour=delta.seconds/3600, minute=((delta.seconds%3600)//60), second=((delta.seconds%3600)%60))
else:
# 昨天已經(jīng)過了23:59,因此是今天北京的時間
yesterday_datetime = current_datetime
yesterday_datetime.strftime("%Y-%m-%d %H:%M:%S")
從昨天(2024年5月6號,星期三)19:21:23到2024年5月7號北京時間19:21:23,總共經(jīng)過了8小時53分50秒。
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。