详见:https://www.runoob.com/python3/python3-date-time.html
获取当前时间的两种方法:
#方法1
import time
current_time = time.strftime("%Y-%m-%d, %H:%M:%S",time.localtime())
print(current_time)
#方法2
from datetime import datetime
current_time = datetime.now().strftime("%Y-%m-%d, %H:%M:%S")
print(current_time)
一个小小的时钟:
import time
while True:
current_time = time.strftime("%H:%M:%S",time.localtime()) # 用了方法1
sys.stdout.write(current_time + '\r')
sys.stdout.flush()
time.sleep(1)
定时任务:
settime='03:00' # 订个好时间叫孙子起床
while True:
current_time = time.strftime("%H:%M:%S",time.localtime())
if current_time[:5] == settime:
print('起床了')
break