问题 今天在之前的代码中发现了一个bug,有个计算当前时间减少一个月的函数,其报出下面的异常信息: ValueError: day is out of range for month 看一下代码: import datatime def _last_month(now_time): last_month = now_time.month - 1 last_year = now_time.year if last_month == 0: last_month = 12 last_year -= 1
import datetime import time **datetime转时间戳** In [1]: now = datetime.datetime.now() In [2]: time.mktime(now.timetuple()) Out[2]: 1433501775.0 **datetime转string** In [3]: now.strftime('%Y-%m-%d') Out[3]: '2015-06-05' In [4]: type(now.strftime('%Y-%m-%d