datetime类的timedalte 表示两个时间的差值

datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

均为可选参数。

举例:

#startdate 为某月1号
next_month = startdate + timedelta(days=calendar.monthrange(startdate.year, startdate.month)[1]) #(下月初)
month_end = next_month - timedelta(days=1) #月末
datetime.datetime.strftime  对string类型转换为datetime类型
举例:
datetime.datetime.strftime(startdate, '%Y-%m-%d')

 日历函数calendar的使用

import calendar

获取每月有多少天

monthRange = calendar.monthrange(2018, 10)
print monthRange 

返回的是元祖,第一个值【0】为该月第一天为周几【0-6】,第二个值【1】表示该月天数

 

python-dateutil月份增减

import datetime
from dateutil.relativedelta import relativedelta

datetime_now = datetime.datetime.now()
datetime_three_month_ago = datetime_now - relativedelta(months=3)
print datetime_three_month_ago

 

相关文章:

  • 2022-12-23
  • 2021-07-09
  • 2022-12-23
  • 2021-04-13
  • 2021-10-08
  • 2022-12-23
  • 2021-08-26
猜你喜欢
  • 2021-08-30
  • 2022-02-20
  • 2021-10-06
  • 2021-09-22
  • 2022-12-23
  • 2021-09-22
相关资源
相似解决方案