1、獲取當前時間戳
float_time = time.time()
2、格式化當前時間
#格式化當前時區時間 now_time = time.strftime('%Y-%m-%d %H:%M:%S') print(now_time)
3、時間戳轉時間
#時間戳轉為本地時間 local = time.localtime(1505284047.1286137) #時間戳轉格林尼治時間 local_s =time.gmtime(1505284047.1286137) pub_st = time.strftime('%Y-%m-%d %H:%M:%S',local_s) pub_time = time.strftime('%Y-%m-%d %H:%M:%S',local) print(pub_time,pub_st)
4、字串轉時間
#字串轉化為時間戳 #pub_time為時間字串 time_str為time型別的時間 time_str = time.strptime(pub_time,'%Y-%m-%d %H:%M:%S') #ss為時間戳 ss = str(time.mktime(time_str)) #sss為格式化時間 sss = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(float(ss))) print(time_str,ss,sss)
5、%b表示英文縮寫月份
hj = '31 Jul 2008' time_str = time.strptime(hj,'%d %b %Y') ss = str(time.mktime(time_str)) sss = time.strftime('%Y-%m-%d',time.localtime(float(ss))) print(time_str,sss)
6、英文對應月份字典
time_dict = {'January':'01','February':'02','March':'03','April':'04','May':'05', 'June':'06','July':'07','August':'08','September':'09','October':'10','November':'11','December':'12'}