风火编程--python时间模块datetime和time

版权声明:风火编程, 欢迎指正. https://blog.csdn.net/weixin_42620314/article/details/82902236

python时间处理模块datetime和time

datetime格式与str的常规转换

将datetime格式转成字符串可以用str(), strftime(t, “format”)
将字符串转回datetime必须使用strptime(t, “format”)

其他情况

获取当天的最后时刻24:00:00

datetime.datetime.combine(datetime.date.tody(), datetime.time.max)

处理12小时(AM/PM)的方法

start_time = "2018-09-29-1点12分"
st = re.sub(r'[点分]', "", start_time).replace("下午", "PM").replace("上午", "AM")
# %p代表"AM"/"PM", %I代表小时(1-12)
stt = datetime.datetime.strptime(st, "%Y-%m-%d-%p-%I-%M")

猜你喜欢

转载自blog.csdn.net/weixin_42620314/article/details/82902236