• 时间的转换:

    时间戳转日期(datetime.date.fromtimestamp(1234567896)),返回日期年-月-日

    时间戳转年月日时分秒(datetime.datetime.fromtimestamp(1234567896)),返回年-月-日 时:分:秒

    年月日时分秒 转换为时间戳  

import datetime
import time
# 获取当前时间
dtime = datetime.datetime.now()
un_time = time.mktime(dtime.timetuple())
print(un_time)
# 将unix时间戳转换为“当前时间”格式
times = datetime.datetime.fromtimestamp(un_time)
print(times)

转换结果:
1559568302.0
2019-06-03 21:25:02

  

    字符串转日期(datetime.datetime.strptime("2020/12/29 8:8:00",'%Y/%m/%d %H:%M:%S'));

  • 当前时间的计算:datetime.date.today()获取当前日志、datetime.datetime.now()获取当前年月日时分秒;
  • 计算当前时间的年、月、日、时、分、秒、星期:直接通过时间对象的属性提取即可;
  • 时间差的运用:如:五天前的日期before_5_date = datetime.date.today() + datetime.timedelta(days=-5)可传入的参数如下:days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0

参考资料:

https://zhuanlan.zhihu.com/p/391900039

https://m.py.cn/jishu/jichu/23538.html

posted on 2023-03-16 17:02  木木-林  阅读(68)  评论(0编辑  收藏  举报