1. 程式人生 > >Python呼叫time模組設定當前時間-指定時間

Python呼叫time模組設定當前時間-指定時間

import datetime
import time
#新建元旦時間
#將程式打包
def A():
# 設定時間
newyear =datetime.datetime(2033,10,1)
#呼叫當前時間
nowtime=datetime.datetime.now()
#計算時間差
#計算設定的時間減去現在的時間等於多少天
delte =newyear - nowtime
#將算出的天數除於365天,則是剩餘年數
year=int(delte.days/365)
#將計算出的天數用%算出餘數則是,算除n年多少天
day=int(delte.days%365)
#計算小時
hour=int(delte.seconds/60/60)
#計算分
minute=int((delte.seconds - hour*60*60)/60)
#計算秒
seconds = delte.seconds - hour*60*60 - minute*60
#輸出
print("距離 2033年10月1日\t剩餘:%d年,%d天,%d時,%d分,%d秒" % (year,day, hour, minute, seconds))
#打包設定迴圈,迴圈輸出剩餘時間,sleep(1):美妙迴圈一次
while True:
time.sleep(1)
A()