datetime 计算时间差

 

 计算时间差:

  .date()       # 格式化

  .timedelta()         # 时间差

import datetime

# 今天
today = datetime.datetime.now()
print(today)  # 2019-03-11 16:24:23.792301

today = datetime.datetime.now().date() 
print(today)  # 2019-03-11

# 昨天
yesterday = datetime.datetime.now().date() - datetime.timedelta(days=1)
print(yesterday)  # 2019-03-10

# 一周前
weekdelta = datetime.datetime.now().date() - datetime.timedelta(weeks=1)
print(weekdelta)  # 2019-03-04

# 一个月前
monthago = datetime.datetime.now().date() - datetime.timedelta(weeks=4)
print(monthago)  # 2019-02-11

 

 

 参考博客:

  https://www.cnblogs.com/amber-liu/p/10120503.html

  1. time模块官方文档
  2. datetime模块官方文档
  3. 廖雪峰的官方网站
  4. http://blog.csdn.net/you_are_my_dream/article/details/61616465
  5. http://blog.csdn.net/tigerking1017/article/details/51332220
  6. http://blog.csdn.net/cherdw/article/details/55224209
  7. https://www.cnblogs.com/cindy-cindy/p/6720196.html
  8. https://blog.csdn.net/python3_2017/article/details/78983370
  9. https://blog.csdn.net/qq_25420115/article/details/53149669

 

posted @ 2019-03-11 16:35  葡萄想柠檬  Views(10506)  Comments(0Edit  收藏  举报
目录代码