1、python處理時間常用的模組time和datetime、calendar模組
1)time
時間戳:是指格林威治時間2023年01月01日00時00分00秒(北京時間2023年01月01日08時00分00秒)起至現在的總秒數。
時間元組:即用乙個元組裝起來的9組數字表示時間
t = (2018,6,24,16,56,45,0,0,0) #(年、月、日、時、分、秒、一周的第幾日、一年的第幾日、夏令時<1(夏令時)、0(不是夏令時)、-1(未知),預設 -1>)
#1、時間三種表示方式:
print('當前時間的時間戳:',time.time())
print("獲取當前時間的時間元組",time.localtime())
# 獲取元組的部分資訊
print('獲取元組的部分資訊:',time.localtime().tm_year,
time.localtime().tm_mon,
time.localtime().tm_mday,
time.localtime().tm_wday)
2、三種表示方式相互轉換
# 1、元組轉化為時間戳
print('元組轉化為時間戳',time.mktime((2020,5,17,20,19,40,6,138,0)))
print(time.mktime(time.localtime()))
3)時間格式字串與時間日期的相互轉換
print('轉換為時間字串',datetime.datetime.strftime(datetime.datetime.now(),'%y_%m_%d %h:%m:%s'))
print('時間字串轉化為時間',datetime.datetime.strptime('2020_05_17 20:51:28','%y_%m_%d %h:%m:%s'))
3)calendar日曆模組
import calendar
print("列印年曆",calendar.calendar(2020))
print('列印月曆',calendar.month(2020,5))
print('是否為閏年',calendar.isleap(2020))
# 獲取指定日期是星期幾
print(calendar.weekday(2020,5,16))
print(time.localtime().tm_wday,datetime.datetime.now().weekday())
2、錯誤異常處理
1)捕獲異常語法結構:
try:
執行**塊1
except:
出現異常執行**2
2)常見錯誤型別:
# indentationerror 縮排錯誤
# valueerror 值錯誤
try:
執行**塊1
except 錯誤型別 as 變數:
出現異常執行**2
try:
num1=int(input('請輸入數字:'))
print(num1+10)
except valueerror as error1:
print(f'值型別錯誤:')
except:
print('非值型別錯誤!')
3)try
執行**塊1
except
**塊1異常執行**
else
**塊1無異常執行**
案例:try:
num1=int(input('請輸入數字:'))
print(num1+10)
except valueerror as error1:
print(f'值型別錯誤:')
except exception as error2:
print(f'非值型別錯誤!,錯誤資訊')
else:
print('輸入的格式為數字')
4)try
執行**塊1
except
**塊1異常執行**
else
**塊1無異常執行**
finally:
不論是否異常都會執行**塊
案例:try:
num1=int(input('請輸入數字:'))
print(num1+10)
except valueerror as error1:
print(f'值型別錯誤:')
except exception as error2:
print(f'非值型別錯誤!,錯誤資訊')
else:
print('輸入的格式為數字')
finally:
print('格式檢驗完成')
iOS 日曆時間日期的用法總結
重寫time的getter方法,獲取時間 nsstring time return dateformatter nsdate與nsdateformatter的相關用法 1.nsdateformatter配合nsdate與nsstring之間的轉化 nsdateformatter有下面2個方法 nss...
5 時間日期和數字
date類 格式化規則 dateformat dateformat y yy 兩位的年份,yyyy為四位的年份 m mm 兩位的月份,mmm為漢字月份 d dd 兩位日期 h hh 兩位小時 m mm 兩位分數 s ss 兩位秒 e 字串輸出星期 calendar類 主要配個date計算。calen...
vb6 0 時間日期
使用year now 可以得到4位數的年 你還可以用format來得到,還有formatdatetime 下面兩種都是一樣的結果 formatdatetime now,vblongdate formatdatetime now,1 下面是第二個引數相關的說明 vbgeneraldate 0 顯示日期...