python日期处理主要有两个模块:time、datetime
time 模块
- 生成当前时间戳
time.time()
- 将时间戳转换为指定格式
time.strftime("%Y/%m/%d %H:%M:%S",time.localtime(time_stamp_str))
- 生成当前时间
time.localtime()
- 将时间转换为指定格式
time.strftime("%Y%m%d %H:%M:%S", time.localtime())
- 已知时间字符串,将其转换为时间格式
time.strftime("%Y%m%d", time.strptime("2020-06-26 22:00:24", "%Y-%m-%d %H:%M:%S"))
- 已知日期转换为时间戳
time.mktime(time.strptime("2020-06-26 22:00:24", "%Y-%m-%d %H:%M:%S"))
import time
# 生成时间戳
unix_time = time.time()
print(unix_time) # 1593179664.8451114
print(type(unix_time)) # <class 'float'>
print(int(unix_time)) # 1593179664
# 已知时间戳,转换成固定格式的日期
time_stamp = 1593179664
time_array = time.localtime(time_stamp) # 格式化时间戳为本地时间
format_time = time.strftime("%Y/%m/%d %H:%M:%S",time_array) # 2020/06/26 21:54:24
# 生成时间数组,即:
time_array = time.localtime() # time.struct_time(tm_year=2020, tm_mon=6, tm_mday=26, tm_hour=22, tm_min=10, tm_sec=22, tm_wday=4, tm_yday=178, tm_isdst=0)
print(time_array)
print(time_array.tm_gmtoff) # "offset from UTC in seconds
print(time_array.tm_year)
print(time_array.tm_mon)
# 已知时间字符串和格式,转换成时间格式
print(time.strptime("2020-06-26 22:00:24", "%Y-%m-%d %H:%M:%S"))
# time.struct_time(tm_year=2020, tm_mon=6, tm_mday=26, tm_hour=22, tm_min=0, tm_sec=24, tm_wday=4, tm_yday=178, tm_isdst=-1)
# 获取固定格式的当前时间
format_time = time.strftime("%Y-%m-%d", time.localtime()) # 2020-06-26
format_time = time.strftime("%Y%m%d %H:%M:%S", time.localtime()) # 20200626 21:59:56
format_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 2020-06-26 22:00:24
# 已知时间字符串和格式,转换成其他时间格式
format_time = time.strftime("%Y%m%d", time.strptime("2020-06-26 22:00:24", "%Y-%m-%d %H:%M:%S"))
print(format_time)
# 已知日期,转换成时间戳
time_stamp = time.mktime(time.strptime("2020-06-26 22:00:24", "%Y-%m-%d %H:%M:%S")) # 1593180024.0
time_stamp = time.mktime(time.strptime("2020-06-26", "%Y-%m-%d")) # 1593100800.0
datetime 模块
1、创建指定时间 datetime.datetime(2020, 6, 26, 8, 30, 30)
2、获取当前时间 datetime.datetime.now()
3、获取当前日期 datetime.datetime.today()
4、时间戳转换为时间 datetime.datetime.fromtimestamp(time_stamp_str)
import datetime
# 用指定日期时间创建datetime
dt = datetime.datetime(2020, 6, 26, 8, 30, 30) # 2020-06-26 08:30:30
# 把datetime转换为timestamp
time_stamp = dt.timestamp() # 1593131430.0
# 获取当前时间
now = datetime.datetime.now() # 2020-06-26 22:27:36.202601
print(type(now)) # <class 'datetime.datetime'>
print(now) # 2020-06-26 22:38:05.514118
print(now.date()) # 2020-06-26
print(now.day) # 26
# 获取当天日期
print(datetime.datetime.today())
# 格式转换,strftime需要为datetime.datetime格式日期才能直接调用
format_time = now.strftime("%Y-%m-%d %H:%M:%S") # 2020-06-26 22:59:22
format_time = datetime.datetime.strftime(now, "%Y/%m/%d %H:%M:%S") # 2020/06/26 22:59:22
# 时间戳转换为时间
time_stamp = 1593179664
d = datetime.datetime.fromtimestamp(time_stamp) # 2020-06-26 21:54:24
d = datetime.datetime.fromtimestamp(time_stamp).strftime("%x") # 06/26/20
d = datetime.datetime.fromtimestamp(time_stamp).strftime("%c") # Fri Jun 26 21:54:24 2020
d = datetime.datetime.fromtimestamp(time_stamp).date() # 2020-06-26
日期格式设置
strftime函数是可以把时间类型值转换为指定格式的;
strptime可以把时间字符串转换为时间类型
常用的格式如下:
'''
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
'''