python技巧早于或晚于指定时间

python时间早晚比较,指定在早于或晚于某个指定时间,执行一段代码。

import datetime 
  
# date and time in yyyy/mm/dd hh:mm:ss format 
d1 = datetime.datetime(2020, 5, 13, 22, 50, 55) 
d2 = datetime.datetime(2020, 7, 13, 22, 50, 55)
d3 = datetime.datetime(2020, 6, 13, 22, 50, 55)

# print(d1 > d2)
# print(d2 > d3)

def todayAt (hr, min=0, sec=0, micros=0):
   now = datetime.datetime.now()
   print(now)
   print(now.replace(hour=hr, minute=min, second=sec, microsecond=micros))
   return now.replace(hour=hr, minute=min, second=sec, microsecond=micros)    

# 用例1:
print(todayAt(14,25))
print(todayAt(17), todayAt(17, 15))

#用例2:    
timeNow = datetime.datetime.now()
if timeNow < todayAt (13):
   print("还未到指定时间...")\

print('>>>>>>>>>>>>>>>>>>>>>')
now = datetime.datetime.now()
print(now)
start_dt = now.replace(hour=19, minute=15, second=0, microsecond=0)
print(start_dt)
print(now>start_dt)

代码输出结果:

2022-06-06 17:19:43.805428
2022-06-06 14:25:00
2022-06-06 14:25:00
2022-06-06 17:19:43.805428
2022-06-06 13:00:00
>>>>>>>>>>>>>>>>>>>>>
2022-06-06 17:19:43.806431
2022-06-06 19:15:00
False

python技巧早于或晚于指定时间

发布者:股市刺客,转载请注明出处:https://www.95sca.cn/archives/76365
站内所有文章皆来自网络转载或读者投稿,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!

(0)
股市刺客的头像股市刺客
上一篇 2024 年 7 月 11 日
下一篇 2024 年 7 月 11 日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注