時間模塊datetime

#時間模塊 datetime
import datetime
#獲取當前時間
cuttent_time=datetime.datetime.now()
print(cuttent_time)

#只獲取年月日
cuttent_day=datetime.date.today()
print(cuttent_day)

#獲取明天的日期
tomorrow=datetime.date.today()+datetime.timedelta(days=1)
print(tomorrow)

#獲取明天的幾個小時後
tomorrow=datetime.datetime.today()+datetime.timedelta(days=1,hours=2)
print(tomorrow)

#獲取昨天幾小時前
yesterday=datetime.datetime.today()-datetime.timedelta(days=1,hours=3)
print(yesterday)

#自定義時間
cus_time=datetime.datetime(2018,5,20,12,24,30)
#與當前時間的時間差
time_diff=cus_time-datetime.datetime.now()
print(time_diff)

#差的天數
days=time_diff.days
print(days)

#差的秒數
seconds=time_diff.total_seconds()
print(seconds)

#格式化的操作
cus_time2=datetime.datetime(2018,6,20,23,12,23)
format_str=cus_time2.strftime("%Y/%m/%d %I/%M/%S")
print(format_str)

#逆向
time_cus=datetime.datetime.strptime(format_str,"%Y/%m/%d %I/%M/%S")
print(time_cus)

#將datetime的時間轉化爲時間元組
tuple_time=cus_time2.timetuple()
print(tuple_time)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章