我必须创建一个基于当前时间的两个时隙,假设当前时间是11:25,所以根据当前时间,我必须创建两个时间段30分钟的时隙,假设是11:00到11:30,另一个是11:30到12:00,而这些时隙应该是动态的,所以如果时间是12:20,那么我的新插槽应该是12:00到12:30和12:30到1:00,只有两个使用python的时隙。
我是蟒蛇新手,任何帮助都将不胜感激。
todayDate = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(todayDate)
currentTime = datetime.now()
now= 30
正如我说的,我是python的新手,我已经尝试了这么多,并且坚持逻辑。
发布于 2022-03-30 03:12:35
你可以使用Pandas模块。特别是date_range函数。
import pandas
# Get current time
now = pandas.Timestamp.now()
# Round current time down to the nearest 30 minutes.
now = now.floor('30min')
# create a range of 3 datetime objects with a frequency of 30 minutes.
datetimes = pandas.date_range(start=now, periods=3, freq='30min')
# Get only the Hour and Minute of those datetime objects.
times = [datetime.strftime("%H:%M") for datetime in datetimes]
# Use list slicing to repeat second time.
times = times[:2] + times[1:]
print(times)
返回
['12:00', '12:30', '12:30', '13:30']
在12点到12点29之间的任何时间运行。
https://stackoverflow.com/questions/71673239
复制相似问题