如何获得本周周一 6 点 30 分的时间?
How to get this week Monday 6 30 AM time?
我是 运行 一个 python 脚本,需要从上周一 6:30 上午开始获取数据。
例如:
if current time,2021/6/28(Monday) 15:00PM > required time is 2021/6/28(Monday) 06:30AM
if current time,2021/7/02(Friday) 15:00PM > required time is 2021/6/28(Monday) 06:30AM
我如何得到这个?
Here is a related question 这可能是解决您的问题的一个很好的起点。
import datetime
now = datetime.datetime.now()
print(now.strftime("%A"))
输出:'Monday'
编辑:
import datetime
days = {'Monday':7, 'Tuesday':1,'Wednesday':2,'Thursday':3,'Friday':4, 'Saturday':5, 'Sunday':6}
now = datetime.datetime.now()
current_day_name = now.strftime("%A")
date_threshold = now.replace(hour=18, minute=30, second=0)
#Handle case of datetime is monday and time is superiro to 6:30
if (current_day_name == 'Monday' and (now > date_threshold ) ):
answer = now.replace(hour=18, minute=30, second=0)
else :
last_week = datetime.timedelta(days=days[current_day_name])
answer = now - last_week
answer = answer.replace(hour=18, minute=30, second=0)
print(answer)
这段代码应该可以解决问题。
我是 运行 一个 python 脚本,需要从上周一 6:30 上午开始获取数据。
例如:
if current time,2021/6/28(Monday) 15:00PM > required time is 2021/6/28(Monday) 06:30AM
if current time,2021/7/02(Friday) 15:00PM > required time is 2021/6/28(Monday) 06:30AM
我如何得到这个?
Here is a related question 这可能是解决您的问题的一个很好的起点。
import datetime
now = datetime.datetime.now()
print(now.strftime("%A"))
输出:'Monday'
编辑:
import datetime
days = {'Monday':7, 'Tuesday':1,'Wednesday':2,'Thursday':3,'Friday':4, 'Saturday':5, 'Sunday':6}
now = datetime.datetime.now()
current_day_name = now.strftime("%A")
date_threshold = now.replace(hour=18, minute=30, second=0)
#Handle case of datetime is monday and time is superiro to 6:30
if (current_day_name == 'Monday' and (now > date_threshold ) ):
answer = now.replace(hour=18, minute=30, second=0)
else :
last_week = datetime.timedelta(days=days[current_day_name])
answer = now - last_week
answer = answer.replace(hour=18, minute=30, second=0)
print(answer)
这段代码应该可以解决问题。