python datetime和time 详解

datetime

datetime.date

d = datetime.date.today()
print(d, type(d))  # 2023-09-23 <class 'datetime.date'>
d = datetime.date(2020, 8, 24)
print(d, type(d))  # 2020-08-24 <class 'datetime.date'>
d = datetime.date.fromtimestamp(time.time())
print(d)  # 2023-09-23   时间戳时间转化成时间
print(d.timetuple())
# time.struct_time(tm_year=2023, tm_mon=9, tm_mday=23, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=266, tm_isdst=-1)
print(d.replace(2024))  # 2024-09-23
print(d.replace(month=8))  # 2023-08-23
print(d.isoweekday())
print(d.strftime("%Y%m%d"))  # 20230923
# d=datetime.datetime.fromtimestamp(time.time()).strftime("%Y%m%d%H%M%S%f")
# print(d) #20230923200049776878

datetime.time 

t = datetime.time(15, 10, 45, 888888)
print(t)  # 15:10:45.888888

print(t.strftime("%H%M%S%f"))  # 151045888888

datetime.datetime

dt = datetime.datetime(2020, 2, 2, 11, 22, 34, 888888)
print(dt)
dt = datetime.datetime.today()
print(dt)
dt = datetime.datetime.utcnow()
print(dt)
# 时间戳---->dt
dt = datetime.datetime.fromtimestamp(150000000)
print(dt)  # 1974-10-03 10:40:00

dt = datetime.datetime.fromtimestamp(time.time())
print(dt)  # 2023-09-23 20:08:23.301262
# 字符串 ->dt
dt = datetime.datetime.strptime("2023-09-23 20:08:23", "%Y-%m-%d %H:%M:%S")
print(dt, type(dt))
# date ,time -->dt
dt = datetime.datetime.combine(d, t)
print(dt)  # 2023-09-23 15:10:45.888888
# dt->结构化对象
print(
    dt.timetuple())  # time.struct_time(tm_year=2023, tm_mon=9, tm_mday=23, tm_hour=15, tm_min=10, tm_sec=45, tm_wday=5, tm_yday=266, tm_isdst=-1)

# dt->时间戳
print(dt.timestamp())
# dt->字符串
dtt = dt.strftime("%Y-%m-%d %H:%M:%S")
print(dtt, type(dtt))  # 2023-09-23 15:10:45   <class 'str'>

# 时间戳-->dt
dt = datetime.datetime.fromtimestamp(time.time())
print(dt, type(dt))  # 2023-09-23 20:24:30.269954 <class 'datetime.datetime'>
# 格式化对象--->dt
dt = datetime.datetime.strptime("2023-09-23 20:08:23", "%Y-%m-%d %H:%M:%S")
print(dt, type(dt))  # 2023-09-23 20:08:23 <class 'datetime.datetime'>

datetime.timedelta

# #cls, days=0, seconds=0, microseconds=0,
#                 milliseconds=0, minutes=0, hours=0, weeks=0
# 生产时间差
td = datetime.timedelta(days=10)
print(td,type(td)) #10 days, 0:00:00 <class 'datetime.timedelta'>
#计算目标是日期
td=datetime.datetime.today()
td.strftime("%Y-%m-%d %H:%M:%S")
print(f'现在是{td}') #现在是2023-09-23 21:02:43.875903
dalta=datetime.timedelta(days=10)
target=td+dalta
print(f'十天后{target}') #十天后2023-10-03 21:02:43.875903
dalta=datetime.timedelta(days=-10)
target=td+dalta
print(f'十天前{target}') #十天前2023-09-13 21:03:57.211239

#计算时间差
dt1=datetime.datetime.today()
dt2=datetime.datetime.utcnow()
td=dt1-dt2
a=str(td.seconds/3600)
print(td,type(td))  # 8:00:00 <class 'datetime.timedelta'>
print(a)

time模块

import time
# 时间戳
print(time.time())  #1695522043.2242804

#结构化时间对象
st=time.localtime()
print(st) #time.struct_time(tm_year=2023, tm_mon=9, tm_mday=24, tm_hour=10, tm_min=29, tm_sec=12, tm_wday=6, tm_yday=267, tm_isdst=0)

#格式化时间
# print(time.ctime())
print(time.strftime("%Y%m%d%H%M%S")) #20230924103617


#sleep
# time.sleep(1)

#三种格式转换
#时间戳-->结构化对象
#UTC
print(time.gmtime())
#time.struct_time(tm_year=2023, tm_mon=9, tm_mday=24, tm_hour=2, tm_min=43, tm_sec=1, tm_wday=6, tm_yday=267, tm_isdst=0)
print(time.gmtime(150000000))
#time.struct_time(tm_year=1974, tm_mon=10, tm_mday=3, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=3, tm_yday=276, tm_isdst=0)
#本地时间
print(time.localtime())
#time.struct_time(tm_year=2023, tm_mon=9, tm_mday=24, tm_hour=10, tm_min=45, tm_sec=15, tm_wday=6, tm_yday=267, tm_isdst=0)
print(time.localtime(150000000))
#time.struct_time(tm_year=1974, tm_mon=10, tm_mday=3, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=3, tm_yday=276, tm_isdst=0)

#结构化对象-->时间戳
print(time.time())
print(time.mktime(time.localtime()))

#结构化对象-->字符串
print(time.strftime("%Y%m%d%H%M%S",time.localtime()))  # 20230924110038


#字符串-> 结构化对象
print(time.strptime("20230924110038","%Y%m%d%H%M%S"))
#time.struct_time(tm_year=2023, tm_mon=9, tm_mday=24, tm_hour=11, tm_min=0, tm_sec=38, tm_wday=6, tm_yday=267, tm_isdst=-1)

datetime模块和time模块正常方式

import time
import datetime
#快速生成时间戳
print(time.time())
#快速生成格式化时间
ret=time.strftime("%Y%m%d%H%M%S",time.localtime())
print(ret,type(ret))

#时间戳 到 时间格式
ret=datetime.datetime.fromtimestamp(time.time())
print(ret,type(ret))  #2023-09-24 11:14:11.546729 <class 'datetime.datetime'>


#格式化时间 到 时间格式
ret=datetime.datetime.strptime("2023-09-23 15:10:45.888888","%Y-%m-%d %H:%M:%S.%f")
print(ret,type(ret)) #2023-09-23 15:10:45.888888 <class 'datetime.datetime'>

ret=datetime.datetime.strptime("20230923151045.888888","%Y%m%d%H%M%S.%f")
print(ret,type(ret)) #2023-09-23 15:10:45.888888 <class 'datetime.datetime'>
#所有字符串格式都可以转化成 时间格式

#时间格式 到格式化时间
dt = datetime.datetime.today()
print(dt,type(dt))  #2023-09-24 11:23:37.284328 <class 'datetime.datetime'>

print(dt.strftime("%Y-%m-%d %H:%M:%S"))   #2023-09-24 11:26:03

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值