datetime时间模块

2022/6/19 23:24:56

本文主要是介绍datetime时间模块,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

datetime模块

datetime.data日期

import datetime

# 相当于datetime.date(2022, 6,19)
d = datetime.date.today()
print(d, type(d))  # 2022-06-19 <class 'datetime.date'>

d = datetime.date(2022, 6, 29)
print(d, type(d))  # 2022-06-29 <class 'datetime.date'>

# 时间戳生成datetime.date类型
import time
s = time.time()     # 时间戳1655636896.815886
d = datetime.date.fromtimestamp(s)
print(d, type(d))   # 2022-06-19 <class 'datetime.date'>

# 类属性
print(datetime.date.min)    # 0001-01-01 最小日期
print(datetime.date.max)    # 9999-12-31 最大日期
print(datetime.date.resolution) # 1 day, 0:00:00 时间间隔单位

# 实例属性
d = datetime.date.today()
print(d.year)   # 2022
print(d.month)  # 6
print(d.day)    # 19

# datetime.date ---> 结构化时间
d = datetime.date.today()
print(d.timetuple())
# time.struct_time(tm_year=2022, tm_mon=6, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=170, tm_isdst=-1)

# 日期替换方法
d = datetime.date.today()
print(d.replace(year=2022, month=11, day=22))   # 2022-11-22

# 日期
d = datetime.date.today()
print(d.isoweekday())   # 7 (1代表周一)
print(d.weekday())      # 6 (0代表周一)
print(d.toordinal())    # 738325 (0001-01-01到今天的天数)

# 格式化
d = datetime.date.today()
print(d.strftime("%Y年%m月%d日"))  # 2022年06月19日

datetime.time时间

import datetime

# 生成时间
# hour minute second microsecond
print(datetime.time())  # 00:00:00
print(datetime.time(15, 30, 30, 999999))  # 15:30:30.999999

# 类方法
print(datetime.time.min)  # 00:00:00
print(datetime.time.max)  # 23:59:59.999999
print(datetime.time.resolution)  # 0:00:00.000001

# 实例属性
t = datetime.time(15, 30, 30, 999999)
print(t.hour)  # 15
print(t.minute)  # 30
print(t.second)  # 30
print(t.microsecond)  # 999999

# 格式化时间
t = datetime.time(15, 30, 30, 999999)
print(t.isoformat())  # 15:30:30.999999标准时间格式
print(t.strftime("%H时%M分%S秒%f微秒"))  # 15时30分30秒999999微秒

datetime.datetime日期时间

import datetime

# 生成日期
# now不加tz,等同于today
dt = datetime.datetime.now(tz=None)
print(dt, type(dt))  # 2022-06-19 19:55:46.275871 <class 'datetime.datetime'>

dt = datetime.datetime.today()
print(dt, type(dt))  # 2022-06-19 19:55:46.275922 <class 'datetime.datetime'>

dt = datetime.datetime(2022, 6, 19, 19, 42, 32, 999999)
print(dt, type(dt))  # 2022-06-19 19:42:32.999999 <class 'datetime.datetime'>

dt = datetime.datetime.today()
print(dt, type(dt))  # 2022-06-19 19:43:17.560803 <class 'datetime.datetime'>

dt = datetime.datetime.now(tz=None)
print(dt, type(dt))  # 2022-06-19 19:44:06.516677 <class 'datetime.datetime'>

dt = datetime.datetime.utcnow()
print(dt, type(dt))  # 2022-06-19 11:44:06.516682 <class 'datetime.datetime'>

# 属性
dt = datetime.datetime.today()
print(dt.year)  # 2022
print(dt.month)  # 6
print(dt.day)  # 19
print(dt.hour)  # 19
print(dt.minute)  # 50
print(dt.second)  # 5

# 时间的加减
# datetime.timedelta() weeks-周/days-天/hours-时/minutes-分/seconds-秒
print(datetime.datetime.now() + datetime.timedelta(days=3))  # 3天后
# 2021-04-09 04:26:18.819308

print(datetime.datetime.now() + datetime.timedelta(days=-3))  # 3天前
# 2021-04-03 04:26:18.819308

# replace替换
dt = datetime.datetime.now()
print(dt.replace(day=1))  # 2022-06-01 20:02:56.127693

datetime.datetime转换

dt <---> 结构化时间

# 时间转换
# dt -> 结构化时间
dt = datetime.datetime.now()
print(dt.timetuple())
# time.struct_time(tm_year=2022, tm_mon=6, tm_mday=19, tm_hour=20, tm_min=4, tm_sec=46, tm_wday=6, tm_yday=170, tm_isdst=-1)


# 结构化时间 -> dt (结构化时间 -> 时间戳 -> dt)
import time
print(datetime.datetime.fromtimestamp(time.mktime(time.localtime())))  # 2022-06-19 20:10:15

dt <---> 时间戳

# dt -> 时间戳
dt = datetime.datetime.now()
print(dt.timestamp())  # 1655640333.800481


# 时间戳 -> dt
import time

dt = datetime.datetime.fromtimestamp(time.time())
print(dt, type(dt))  # 2022-06-19 19:46:13.567158 <class 'datetime.datetime'>

dt = datetime.datetime.utcfromtimestamp(time.time())
print(dt, type(dt))  # 2022-06-19 11:46:46.886705 <class 'datetime.datetime'>

dt <--->字符串

字符串转dt,格式需要一样

# dt -> 字符串
dt = datetime.datetime.now()
print(dt.strftime("%Y-%m-%d %H:%M:%S"))  # 2022-06-19 20:06:49


# 字符串 -> dt
dt = datetime.datetime.strptime("2022-06-19 19:46:13", "%Y-%m-%d %H:%M:%S")
print(dt, type(dt))  # 2022-06-19 19:46:13 <class 'datetime.datetime'>

datetime.timedelta日期加减

# 向前一个星期
print(datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=-1))
# -7 days, 0:00:00

# 向后8天
print(datetime.timedelta(days=1, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=1))
# 8 day, 0:00:00


# 日期加减
dt = datetime.datetime.now()
print(dt + datetime.timedelta(weeks=2))  # 2022-07-03 20:15:30.422769

dt = datetime.datetime.today()
print(dt + datetime.timedelta(weeks=2))  # 2022-07-03 20:15:30.422777


dt = datetime.datetime.now()
print(dt + datetime.timedelta(weeks=-2))  # 2022-06-05 20:15:56.150082

dt = datetime.datetime.today()
print(dt + datetime.timedelta(weeks=-2))  # 2022-06-05 20:15:56.150088


# 时间差
d1 = datetime.datetime.now()
d2 = datetime.datetime(2022, 6, 7, 10, 23, 41, 999999)
print((d1 - d2).days)  # 12
print((d1 - d2).seconds)  # 36040
print((d1 - d2).microseconds)  # 416981


这篇关于datetime时间模块的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程