目录
2)时间戳转为时间元组显示,time.localtime([secs])
4)格式化字符串显示时间 time.strftime(format[,tupletime])
6)将格式字符串转为时间元组 time.strptime(string, format)
7)将格式化时间(struct_time)转为时间戳秒 time.mktime(tupletime)
2)判断传入的日期(与运行时日期比较),是否超过限定天数,如14天
格式化日期和时间
- time 模块和calendar 模块;
- 时间间隔是以秒为单位的浮点小数;
- 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示;
- 时间戳单位可用于日期运算。但是1970年之前的日期就无法以此表示了。太遥远的日期也不行,UNIX和Windows只支持到2038年;
1、Time模块
1)显示当前时间戳,单位秒,time.time()
import time # 引入time模块
# 显示当前时间戳
ticks = time.time()
print("当前时间戳为:", ticks) # 1603932950.558615
输出结果:
当前时间戳为: 1603933083.360215
2)时间戳转为时间元组显示,time.localtime([secs])
- 接收时间戳(1970纪元后经过的浮点秒数)并返回当地时间下的时间元组t
时间元组:struct_time元组
序号 | 字段 | 属性 | 值 |
---|---|---|---|
0 | 4位数年 | tm_year | 2008 |
1 | 月 | tm_mon | 1 到 12 |
2 | 日 | tm_mday | 1到31 |
3 | 小时 | tm_hour | 0到23 |
4 | 分钟 | tm_min | 0到59 |
5 | 秒 | tm_sec | 0到61 (60或61 是闰秒) |
6 | 一周的第几日 | tm_wday | 0到6 (0是周一) |
7 | 一年的第几日 | tm_yday | 1到366 (儒略历) |
8 | 夏令时 | tm_isdst | -1, 0, 1, -1是决定是否为夏令时的旗帜 |
import time
# 从返回浮点数的时间戳方式向时间元组转换
localtime = time.localtime(time.time())
print(type(localtime)) # 时间元组, struct_time元组
print("本地时间为 :", localtime)
print("4位数年tm_year:", localtime[0])
print("2位数月tm_mon:", localtime[1])
print("2位数日tm_mday:", localtime[2])
print("2位数小时tm_hour:", localtime[3])
print("2位数分钟tm_min:", localtime[4])
print("2位数秒tm_sec:", localtime[5])
print("1位数一周的第几日tm_wday:", localtime[6])
print("3位数一年的第几日tm_yday:", localtime[7])
print("1位数夏令时tm_isdst:", localtime[8])
输出结果:
<class 'time.struct_time'>
本地时间为 : time.struct_time(tm_year=2020, tm_mon=10, tm_mday=29, tm_hour=9, tm_min=16, tm_sec=11, tm_wday=3, tm_yday=303, tm_isdst=0)
4位数年tm_year: 2020
2位数月tm_mon: 10
2位数日tm_mday: 29
2位数小时tm_hour: 9
2位数分钟tm_min: 16
2位数秒tm_sec: 11
1位数一周的第几日tm_wday: 3
3位数一年的第几日tm_yday: 303
1位数夏令时tm_isdst: 0
3)获取格式化的时间time.asctime()
import time
localtime = time.asctime(time.localtime(time.time()))
print("本地时间为 :", localtime)
输出结果:
本地时间为 : Thu Oct 29 09:22:40 2020
4)格式化字符串显示时间 time.strftime(format[,tupletime])
- 接收以时间元组,并返回以可读字符串表示的当地时间,格式由fmt决定。
import time
# 格式化成2016-03-20 11:45:39形式
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# 格式化成Sat Mar 28 22:24:24 2016形式
print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
输出结果:
2020-10-29 09:38:43
Thu Oct 29 09:38:43 2020
5)python中时间日期格式化符号
- %y 两位数的年份表示(00-99)
- %Y 四位数的年份表示(000-9999)
- %m 月份(01-12)
- %d 月内中的一天(0-31)
- %H 24小时制小时数(0-23)
- %I 12小时制小时数(01-12)
- %M 分钟数(00-59)
- %S 秒(00-59)
- %a 本地简化星期名称
- %A 本地完整星期名称
- %b 本地简化的月份名称
- %B 本地完整的月份名称
- %c 本地相应的日期表示和时间表示
- %j 年内的一天(001-366)
- %p 本地A.M.或P.M.的等价符
- %U 一年中的星期数(00-53)星期天为星期的开始
- %w 星期(0-6),星期天为星期的开始
- %W 一年中的星期数(00-53)星期一为星期的开始
- %x 本地相应的日期表示
- %X 本地相应的时间表示
- %Z 当前时区的名称
- %% %号本身
6)将格式字符串转为时间元组 time.strptime(string, format)
- 根据format的格式,把一个时间字符串,解析为时间元组。
- 要按照字符串的格式,选择格式化的方式,否则格式不对,会报错;
- 返回时间元组struct_time类型的格式化时间;
格式有:
- format = '%a %b %d %H:%M:%S %Y' --- Sat Mar 28 22:24:24 2016
- format2 = '%Y-%m-%d %H:%M:%S' --- 2016-03-20 11:45:39
import time
# 将格式字符串转换为格式化时间time.strptime(string, format)
str = "Sat Mar 28 22:24:24 2016"
format = '%a %b %d %H:%M:%S %Y' # 要按照字符串的格式,选择格式化的方式
time_tuple = time.strptime(str, format)
print(type(time_tuple))
print(time_tuple)
# 选择错误的格式化方式,会报错
format2 = '%Y-%m-%d %H:%M:%S'
# time_tuple = time.strptime(str, format2) # 报错,ValueError: time data 'Sat Mar 28 22:24:24 2016' does not match format '%Y-%m-%d %H:%M:%S'
# print(time_tuple)
输出结果:返回时间元组struct_time类型的格式化时间
<class 'time.struct_time'>
time.struct_time(tm_year=2016, tm_mon=3, tm_mday=28, tm_hour=22, tm_min=24, tm_sec=24, tm_wday=5, tm_yday=88, tm_isdst=-1)
将字符串转为格式化时间
将"Sat Mar 28 22:24:24 2016"字符串,转为2016-03-28 22:24:24格式
import time
# 将字符串转为格式化时间
str = "Sat Mar 28 22:24:24 2016"
format = '%a %b %d %H:%M:%S %Y' # 要按照字符串的格式,选择格式化的方式
time_tuple = time.strptime(str, format) # 转为时间元组struct_time
# 对时间元组struct_time进行格式化输出
format2 = '%Y-%m-%d %H:%M:%S'
time_format = time.strftime(format2, time_tuple)
print(time_format) # 2016-03-28 22:24:24
输出结果:
2016-03-28 22:24:24
7)将格式化时间(struct_time)转为时间戳秒 time.mktime(tupletime)
- 接受时间元组并返回时间戳(1970纪元后经过的浮点秒数)。
import time
# 将格式化时间(struct_time)转为时间戳
time_tuple = time.localtime() # 当前时间
sec = time.mktime(time_tuple) # 转为时间戳
print(sec) # 1603937866.0
输出结果:
1603937866.0
8)睡眠时间(同步)time.sleep(secs)
- 推迟调用线程的运行,secs指秒数。
import time
# 睡眠10秒
print("开始时间:", time.asctime(time.localtime()))
time.sleep(10)
print("结束时间:", time.asctime(time.localtime()))
输出结果:
开始时间: Thu Oct 29 10:38:45 2020
结束时间: Thu Oct 29 10:38:55 2020
2、日历calendar模块
1)获取某月字符串格式日历
import calendar
# 获取某月日历
cal = calendar.month(2016, 1)
print("以下输出2016年1月份的日历:")
print(cal)
输出结果:
2)闰年判断
import calendar
# 闰年判断
print(calendar.isleap(2000)) # 是闰年,返回True
print(calendar.isleap(1999)) # 不是闰年,返回False
3、datetime模块
1)获取当前日期,获取昨天日期,文件名称后面加上日期时间;
import datetime
class GetDate:
# 返回当前日期
def get_today(self):
today = datetime.date.today() # 格式2020-12-17
return today
# 返回昨天日期
def get_yesterday(self):
today = datetime.date.today()
oneday = datetime.timedelta(days=1)
yesterday = today - oneday # 格式2020-12-16
return yesterday
get_date = GetDate()
if __name__ == '__main__':
today = get_date.get_today()
yesterday = get_date.get_yesterday()
print(today)
print(yesterday)
file_name_date = 'all-subtask' + '_' + str(today)
print(file_name_date)
输出结果:
2020-12-17
2020-12-16
all-subtask_2020-12-17
2)判断传入的日期(与运行时日期比较),是否超过限定天数,如14天
- 超过限定天数返回True,未超过限定天数返回False
import datetime
import time
class DateUtil:
# 判断传入的日期,是否超过限定天数,如14天
def is_passed_limit_days(self, date, limit_days, format='%Y-%m-%d %H:%M:%S'):
time_tuple = time.strptime(date, format)
year = time_tuple.tm_year
month = time_tuple.tm_mon
day = time_tuple.tm_mday
date = datetime.date(year, month, day)
today = datetime.date.today()
days = (today - date).days
print(days)
if int(days) > int(limit_days):
return True
else:
return False
if __name__ == '__main__':
util = DateUtil()
reqs_date = '2020-11-1 16:16:40'
limit_days = 14
is_passed_limit_days = util.is_passed_limit_days(reqs_date, limit_days)
print(is_passed_limit_days)
输出结果:
47
True