time模块内置方法
print([i for i in dir(time) if not i.startswith('_')])
time_func = ['altzone', 'asctime', 'clock', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'monotonic_ns', 'perf_counter', 'perf_counter_ns', 'process_time', 'process_time_ns', 'sleep', 'strftime', 'strptime', 'struct_time', 'thread_time', 'thread_time_ns', 'time', 'time_ns', 'timezone', 'tzname']
程序等待
import time
time.sleep(1) # 程序阻塞等待1秒
print('继续执行')
获取当前时间戳
import time
time_stamp = time.time()
print(time_stamp) # 1617353931.0670428
将时间错转换为结构化时间(time.struct_time)
time.struct_time 类型可以使用的属性为:
import time
time_stamp = time.time() # now为float类型
struct_time = time.localtime(time_stamp)
print(struct_time) # time.struct_time(tm_year=2021, tm_mon=4, tm_mday=2, tm_hour=17, tm_min=4, tm_sec=59, tm_wday=4, tm_yday=92, tm_isdst=0)
将结构化时间转换为时间戳
import time
time_stamp = time.time()
struct_time = time.localtime(time_stamp) # 将时间戳转换为结构化时间
time_stamp = time.mktime(struct_time) # 将结构化时间转换为时间戳
print(time_stamp) # 1617354299.0
时间相互转换 (2021-04-02 17:20:00)
import time
time_stamp = 1617354299 # 时间戳
struct_time = time.localtime(time_stamp) # 时间戳转换为结构化时间
format_time = time.strftime('%Y-%m-%d %H:%M:%S', struct_time) # 将结构化时间转换为格式化时间
struct_time = time.strptime('%Y-%m-%d %H:%M:%S', format_time) # 将格式化时间转换为结构化时间
time_stamp = time.mktime(struct_time) # 将结构化时间转换为时间戳
总结
time.sleep(int) :程序阻塞等待N秒
time.time() : 获取当前时间戳
time.locatime(time_stamp) :时间戳转换为结构化时间
time.mktime(struct_time) : 结构化时间转换为时间戳
time.strftime(str, struct_time): 将结构化时间格式化(2021-04-02 17:20:00)
struct_time = time.strptime(str, format_time) : # 将格式化时间转换为结构化时间(struct_time)
datetime模块其实是一个py文件,该文件中主要有date、time、datetime、timedelta、timezone、tzinfo这几个类。
datetime模块各个类说明
类名 | 说明 |
---|---|
date | 一个理想化的简单型日期。 属性: year,month, day |
time | 一个独立于任何特定日期的理想化时间,它假设每一天都恰好等于 24*60*60 秒。属性:hour, minute, second, microsecond, tzinfo |
datetime | 日期和时间的结合。属性:year,month, day, hour, minute, second, microsecond, tzinfo |
timedelta | 表示两个 date 对象或者 time 对象,或者 datetime 对象之间的时间间隔,精确到微秒 |
tzinfo | 一个描述时区信息对象的抽象基类。 用来给 datetime 和 time 类提供自定义的时间调整概念(例如处理时区和/或夏令时) |
timezone | 一个实现了 tzinfo 抽象基类的子类,用于表示相对于 世界标准时间(UTC)的偏移量。 |
date 类
查看date类型所有的属性方法
from datetime import date
print([i for i in dir(date) if not i.startswith('_')])
date_method = ['ctime', 'day', 'fromisoformat', 'fromordinal', 'fromtimestamp', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'min', 'month', 'replace', 'resolution', 'strftime', 'timetuple', 'today', 'toordinal', 'weekday', 'year']
today() 获取当前日期
from datetime import date
today = date.today()
print(today) # datetime.date(2021, 4, 2)
print(today.year) # 2021
print(today.month) # 4
print(today.day) # 2
print(today.weekday) # 5
fromtimestamp(t) 将时间戳转换为日期格式
from datetime import date
t = 1617354299
stamp_date = date.fromtimestamp(t)
print(stamp_date) # datetime.date(2021, 4, 2)
fromisoformat(date_string) 将字符串转换为日期类型
from datetime import date
date_struct = date.fromtimestamp('2021-04-02')
print(date_struct) # datetime.date(2021, 4, 2)
replace(year=None, month=None, day=None) 修改年月日
from datetime import date
today = date.today()
last_year = today.replace(year=2020)
print(last_year) # datetime.date(2020, 4, 2)
strftime(fmt) 格式化输出(将日期类型转换为字符串)
from datetime import date
today = date.today()
today_format = today.strftime('%Y-%m-%d')
print(today_format) # '2021-04-02'
ctime() 将日期格式化输出
from datetime import date
today = date.today()
today_format = today.ctime()
print(today_format) # 'Fri Apr 2 00:00:00 2021'
timetuple() 将日期转换为time模块中的struct类型
from datetime import date
time_struct = date.today().timetuple()
print(time_struct) # time.struct_time(tm_year=2021, tm_mon=4, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=92, tm_isdst=-1)
isoformat、isocalendar、isoweekday 将日期类型转换为ISO标准的字符串
from datetime import date
today = date.today()
print(today.isoformat()) # '2021-04-02'
print(today.isocalendar()) # (2021, 13, 5)
print(today.isoweekday()) # 5 (该值为星期五)
time 类
时间类所有属性方法
from datetime import time
print([i for i in dir(time) if not i.startswith('_')])
time_methods = ['dst', 'fold', 'fromisoformat', 'hour', 'isoformat', 'max', 'microsecond', 'min', 'minute', 'replace', 'resolution', 'second', 'strftime', 'tzinfo', 'tzname', 'utcoffset']
时间相关属性 (小时、分钟、秒、微妙)
from datetime import time
t = time(hour=10, minute=10, second=10, microsecond=10)
print(t) # datetime.time(10, 10, 10, 10)
print(t.hour) # 10
print(t.minute) # 10
print(t.second) # 10
print(t.microsecond) # 10
print(t.max) # datetime.time(23, 59, 59, 999999)
print(t.min) # datetime.time(0, 0)
replace(hour=None, minute=None, second=None, microsecond=None) 替换(时、分、秒、微妙)
from datetime import time
t = time(hour=10, minute=10, second=10, microsecond=10)
t.replace(hour=11, minute=9, second=2, microsecond=22)
print(t) # datetime.time(11, 9, 2, 22)
isoformat() 将时间格式化
from datetime import time
t = time(hour=10, minute=10, second=10, microsecond=10)
print(t.isoformat()) # '10:10:10.000010'
t = time(hour=10, minute=10, second=10)
print(t.isoformat()) # '10:10:10'
strftime(ftm) 将时间格式化
from datetime import time
t = time(hour=10, minute=10, second=10, microsecond=10)
print(t.strftime('%H:%M:%S')) # '10:10:10'
datetime 时间日期类
该类继承了date类,同时实现啦time中的部门方法,可以说是整合啦,date和time类。
时间日期类所有属性方法
from datetime import datetime
print([i for i in dir(datetime) if not i.startswith('_')])
datetime_methods = ['astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold', 'fromisoformat', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']
属性(year、month、day、hour、minute、second、microsecond)
from datetime import datetime
now = datetime.now()
print(now) # datetime.datetime(2021, 4, 2, 23, 39, 10, 290274)
print(now.year) # 2021
print(now.month) # 4
print(now.day) # 2
print(now.hour) # 23
print(now.minute) # 40
print(now.second) # 39
print(now.microsecond) # 328574
print(now.max) # datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
print(now.min) # datetime.datetime(1, 1, 1, 0, 0)
datetime对象、时间戳、格式化字符串相互转换
from datetime import datetime
now = datetime.now()
# 将时间日期对象转换为时间戳
print(now.timestamp()) # 1617377994.328574
# 将时间戳转换为时间日期对象
print(datetime.fromtimestamp(1617377994)) # datetime.datetime(2021, 4, 2, 23, 39, 54)
# 将时间日期对象格式化输出(转换为字符串)
print(now.strftime('%Y-%m-%d %H:%M:%S')) # '2021-04-02 23:39:54'
# 将字符串转换为时间日期格式
print(datetime.strptime('2021-04-02 19:32:19', '%Y-%m-%d %H:%M:%S')) # datetime.datetime(2021, 4, 2, 19, 32, 19)
# ISO标准格式化,将ISO标准的字符串转换为datetime对象
print(datetime.fromisoformat('2021-04-02 11:23:10')) # datetime.datetime(2021, 4, 2, 11, 23, 10)
# 将datetime对象转换为ISO标准的字符串
print(now.isoformat()) # '2021-04-02T23:39:54.328574'
timedelta 时间间隔类
属性及方法
from datetime import timedelta
print([i for i in dir(timedelta) if not i.startswith('_')])
delta = ['days', 'max', 'microseconds', 'min', 'resolution', 'seconds', 'total_seconds']
简单使用
from datetime import timedelta
from datetime import datetime
now = datetime.now() # 获取当前时间
# timedelta(days=0, seconds=0, microseconds=0,milliseconds=0, minutes=0, hours=0, weeks=0)
delta_1d = timedelta(days=1)
tomorrow = now + delta_1d
print(tomorrow) # datetime.datetime(2021, 4, 4, 0, 11, 12, 604147)