目录
一、时间time
1、struct_time 类
2、常用函数
3、date类
4、time类
5、datetime类
一、时间time
1、struct_time 类
索引 | 属性 | 值 |
0 | tm_year(年) | 如:1945 |
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 |
6 | tm_wday(周) | 0 ~ 6 |
7 | tm_yday(一年内第几天) | 1 ~ 366 |
8 | tm_isdst(夏时令) | -1、0、1 |
import time
t =time.localtime()
print(t)
t1 =t.tm_year
print(t1)
print(t.tm_mon)
print(t.tm_hour)
print(t.tm_zone)
结果
time.struct_time(tm_year=2022, tm_mon=7, tm_mday=6, tm_hour=15, tm_min=46, tm_sec=47, tm_wday=2, tm_yday=187, tm_isdst=0)
2022
7
15
中国标准时间
2、常用函数
函数(常量) | 说明 |
time() | 返回当前时间的时间戳 |
gmtime([secs]) | 将时间戳转换为格林威治天文时间下的 struct_time,可选参数 secs 表示从 epoch 到现在的秒数,默认为当前时间 |
localtime([secs]) | 与 gmtime() 相似,返回当地时间下的 struct_time |
mktime(t) | localtime() 的反函数 |
asctime([t]) | 接收一个 struct_time 表示的时间,返回形式为:Mon Dec 2 08:53:47 2019 的字符串 |
ctime([secs]) | ctime(secs) 相当于 asctime(localtime(secs)) |
strftime(format[, t]) | 格式化日期,接收一个 struct_time 表示的时间,并返回以可读字符串表示的当地时间 |
sleep(secs) | 暂停执行调用线程指定的秒数 |
altzone | 本地 DST 时区的偏移量,以 UTC 为单位的秒数 |
timezone | 本地(非 DST)时区的偏移量,UTC 以西的秒数(西欧大部分地区为负,美国为正,英国为零) |
tzname | 两个字符串的元组:第一个是本地非 DST 时区的名称,第二个是本地 DST 时区的名称 |
import time
print(time.time())
print(time.gmtime())
print(time.localtime())
print(time.asctime(time.localtime()))
print(time.tzname)
# strftime 使用
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
结果
1657093909.1237514
time.struct_time(tm_year=2022, tm_mon=7, tm_mday=6, tm_hour=7, tm_min=51, tm_sec=49, tm_wday=2, tm_yday=187, tm_isdst=0)
time.struct_time(tm_year=2022, tm_mon=7, tm_mday=6, tm_hour=15, tm_min=51, tm_sec=49, tm_wday=2, tm_yday=187, tm_isdst=0)
Wed Jul 6 15:51:49 2022
('中国标准时间', '中国夏令时')
2022-07-06 15:51:49
符号 | 说明 |
%a | 本地化的缩写星期中每日的名称 |
%A | 本地化的星期中每日的完整名称 |
%b | 本地化的月缩写名称 |
%B | 本地化的月完整名称 |
%c | 本地化的适当日期和时间表示 |
%d | 十进制数 [01,31] 表示的月中日 |
%H | 十进制数 [00,23] 表示的小时(24小时制) |
%I | 十进制数 [01,12] 表示的小时(12小时制) |
%j | 十进制数 [001,366] 表示的年中日 |
%m | 十进制数 [01,12] 表示的月 |
%M | 十进制数 [00,59] 表示的分钟 |
%p | 本地化的 AM 或 PM |
%S | 十进制数 [00,61] 表示的秒 |
%U | 十进制数 [00,53] 表示的一年中的周数(星期日作为一周的第一天) |
%w | 十进制数 [0(星期日),6] 表示的周中日 |
%W | 十进制数 [00,53] 表示的一年中的周数(星期一作为一周的第一天) |
%x | 本地化的适当日期表示 |
%X | 本地化的适当时间表示 |
%y | 十进制数 [00,99] 表示的没有世纪的年份 |
%Y | 十进制数表示的带世纪的年份 |
%z | 时区偏移以格式 +HHMM 或 -HHMM 形式的 UTC/GMT 的正或负时差指示,其中 H 表示十进制小时数字,M 表示小数分钟数字 [-23:59, +23:59] |
%Z | 时区名称 |
%% | 字面的 '%' 字符 |
3、date类
方法(属性) | 说明 |
today() | 返回当地的当前日期 |
fromtimestamp(timestamp) | 根据给定的时间戮,返回本地日期 |
min | date 所能表示的最小日期 |
max | date 所能表示的最大日期 |
import datetime
import time
print(datetime.date.today())
print(datetime.date.fromtimestamp(time.time()))
print(datetime.date.min)
print(datetime.date.max)
结果
2022-07-06
2022-07-06
0001-01-01
9999-12-31
方法(属性) | 说明 |
replace(year, month, day) | 生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性 |
timetuple() | 返回日期对应的 struct_time 对象 |
weekday() | 返回一个整数代表星期几,星期一为 0,星期天为 6 |
isoweekday() | 返回一个整数代表星期几,星期一为 1,星期天为 7 |
isocalendar() | 返回格式为 (year,month,day) 的元组 |
isoformat() | 返回格式如 YYYY-MM-DD 的字符串 |
strftime(format) | 返回自定义格式的字符串 |
year | 年 |
month | 月 |
day | 日 |
import datetime
td = datetime.date.today()
print(td.replace(year=1945, month=8, day=15))
print(td.timetuple())
print(td.weekday())
print(td.isoweekday())
print(td.isocalendar())
print(td.isoformat())
print(td.strftime('%Y %m %d %H:%M:%S %f'))
print(td.year)
print(td.month)
print(td.day)
结果
1945-08-15
time.struct_time(tm_year=2022, tm_mon=7, tm_mday=6, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=187, tm_isdst=-1)
2
3
(2022, 27, 3)
2022-07-06
2022 07 06 00:00:00 000000
2022
7
6
4、time类
方法(属性) | 说明 |
isoformat() | 返回 HH:MM:SS 格式的字符串 |
replace(hour, minute, second, microsecond, tzinfo, * fold=0) | 创建一个新的时间对象,用参数指定的时、分、秒、微秒代替原有对象中的属性 |
strftime(format) | 返回自定义格式的字符串 |
hour | 时 |
minute | 分 |
second | 秒 |
microsecond | 微秒 |
tzinfo | 时区 |
import datetime
t = datetime.time(10, 10, 10)
print(t.isoformat())
print(t.replace(hour=9, minute=9))
print(t.strftime('%I:%M:%S %p'))
print(t.hour)
print(t.minute)
print(t.second)
print(t.microsecond)
print(t.tzinfo)
结果
10:10:10
09:09:10
10:10:10 AM
10
10
10
0
None
5、datetime类
方法(属性) | 说明 |
today() | 返回当地的当前时间 |
now(tz=None) | 类似于 today(),可选参数 tz 可指定时区 |
utcnow() | 返回当前 UTC 时间 |
fromtimestamp(timestamp, tz=None) | 根据时间戳返回对应时间 |
utcfromtimestamp(timestamp) | 根据时间戳返回对应 UTC 时间 |
combine(date, time) | 根据 date 和 time 返回对应时间 |
min | datetime 所能表示的最小日期 |
max | datetime 所能表示的最大日期 |
import datetime
print(datetime.datetime.today())
print(datetime.datetime.now())
print(datetime.datetime.utcnow())
print(datetime.datetime.fromtimestamp(time.time()))
print(datetime.datetime.utcfromtimestamp(time.time()))
print(datetime.datetime.combine(datetime.date(2019, 12, 1), datetime.time(10, 10, 10)))
print(datetime.datetime.min)
print(datetime.datetime.max)
方法(属性) | 说明 |
date() | 返回具有同样 year,month,day 值的 date 对象 |
time() | 返回具有同样 hour, minute, second, microsecond 和 fold 值的 time 对象 |
replace(year, month, day=self.day, hour, minute, second, microsecond, tzinfo, * fold=0) | 生成一个新的日期对象,用参数指定的年,月,日,时,分,秒...代替原有对象中的属性 |
weekday() | 返回一个整数代表星期几,星期一为 0,星期天为 6 |
isoweekday() | 返回一个整数代表星期几,星期一为 1,星期天为 7 |
isocalendar() | 返回格式为 (year,month,day) 的元组 |
isoformat() | 返回一个以 ISO 8601 格式表示日期和时间的字符串 YYYY-MM-DDTHH:MM:SS.ffffff |
strftime(format) | 返回自定义格式的字符串 |
year | 年 |
month | 月 |
day | 日 |
hour | 时 |
minute | 分 |
second | 秒 |
microsecond | 微秒 |
tzinfo | 时区 |
import datetime
td = datetime.datetime.today()
print(td.date())
print(td.time())
print(td.replace(day=11, second=10))
print(td.weekday())
print(td.isoweekday())
print(td.isocalendar())
print(td.isoformat())
print(td.strftime('%Y-%m-%d %H:%M:%S .%f'))
print(td.year)
print(td.month)
print(td.month)
print(td.hour)
print(td.minute)
print(td.second)
print(td.microsecond)
print(td.tzinfo)
Python 基础(八):与时间相关的模块