目录
一、时间time模块
1.三种时间的表达方式
时间的三种形式:
(1) 时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
(2) 格式化的时间字符串(Format String): ‘1988-03-16’
(3) 元组(struct_time) :struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)
import time
#1.时间戳
print(time.time())
# 1636822287.2530656
#2.时间字符串
print(time.strftime('%Y/%m/%d %H:%M:%S'))
print(time.strftime('%Y/%m/%d %X')) #%H:%M:%S也可以使用%X代替
#2021/11/14 00:53:17
#3.时间元组
print(time.localtime())
#time.struct_time(tm_year=2021, tm_mon=11, tm_mday=14, tm_hour=0, tm_min=58, tm_sec=45, tm_wday=6, tm_yday=318, tm_isdst=0)
小结:时间戳是计算机能够识别的时间;时间字符串是人能够看懂的时间;元组则是用来操作时间的
2.三种时间表达方式的相互转换
#三种时间表达方式的相互转换
#1.时间戳转换成时间元组
t1 = time.localtime(5000)
print(t1)
#2.时间元组转换成时间戳
t2 = time.mktime(t1)
print(t2)
#3.时间元组转换成字符串时间
t3 = time.strftime('%Y/%m/%d %X %a')
print(t3)
#4.字符串时间转换成时间元组
t4 = time.strptime('2021/11/14','%Y/%m/%d')
print(t4)
3.时间的其他格式
import time
#将时间元组转换成"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串
t5 = time.asctime(time.localtime(312343423))
print(t5)
#'Sun Nov 25 10:03:43 1979'
#将时间戳转换成"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串
t6 = time.ctime(312343423)
print(t6)
#'Sun Nov 25 10:03:43 1979'
二、日期datetime模块
1. datetime模块有一下几个类
类名称 | 描述 |
---|---|
datetime.date | 表示日期,常用的属性有:year, month和day |
datetime.time | 表示时间,常用属性有:hour, minute, second, microsecond |
datetime.datetime | 表示日期时间 |
datetime.timedelta | 表示两个date、time、datetime实例之间的时间间隔,分辨率(最小单位)可达到微秒 |
2.datetime类
class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
类方法/属性名称 | 描述 |
---|---|
datetime.today() | 返回一个表示当前本期日期时间的datetime对象 |
datetime.now([tz]) | 返回指定时区日期时间的datetime对象,如果不指定tz参数则结果同上 |
datetime.utcnow() | 返回当前utc日期时间的datetime对象 |
datetime.fromtimestamp(timestamp[, tz]) | 根据指定的时间戳创建一个datetime对象 |
datetime.utcfromtimestamp(timestamp) | 根据指定的时间戳创建一个datetime对象 |
datetime.strptime(date_str, format) | 将时间字符串转换为datetime对象 |
对象方法和属性:
对象方法/属性名称 | 描述 |
---|---|
dt.year, dt.month, dt.day | 年、月、日 |
dt.hour, dt.minute, dt.second | 时、分、秒 |
dt.date() | 获取datetime对象对应的date对象 |
dt.time() | 获取datetime对象对应的time对象, tzinfo 为None |
dt.isoformat([sep]) | 返回一个‘%Y-%m-%d |
dt.strftime(format) | 返回指定格式的时间字符串 |
import datetime
#datetime类型
now = datetime.datetime.now()
print(now,type(now))
today = datetime.datetime.today()
print(today,type(today))
#转换成字符串
d1 = datetime.datetime.now().strftime('%Y/%m/%d')
print(d1,type(d1))
3.date和time类
import datetime
#date类型
today = datetime.date.today()
print(today,type(today))
#time类型
now = datetime.time(20,12,12)
print(now,type(now))
print(now.hour)
print(now.minute)
print(now.second)
4.datetime.timedelta类
import datetime
#datetime.timedelta类
now = datetime.datetime.now()
print(now)
print(now+datetime.timedelta(days=3))
print(now-datetime.timedelta(days=3))
补充:datetime.timedelta类不能做 月 的加减法,可以使用 dateutil.relatived.relativedelta类
三、实际应用
1.获取当天23:59:59的毫秒级时间戳
import datetime
now = datetime.datetime.now() # 当前时间的datetime类型
micro_zero_today = int((now - timedelta(hours=now.hour, minutes=now.minute, seconds=now.second, microseconds=now.microsecond)).timestamp())*1000
micro_last_today = micro_zero_today + 86399999