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'}