python减去datetime对象的月份(python substract months from datetime object)
我想减去几个月,并以yyyymmdd的形式得到一个结果。 为了开始我给我一个字符串时间戳以秒为单位,那么这就是我正在做的事情:
>>> ts=1454284800
>>> billdate = datetime.datetime.strptime(time.strftime('%Y%m%d', time.gmtime(ts)), '%Y%m%d') - datetime.timedelta(5*365/12.0)
>>> billdate
datetime.datetime(2015, 9, 1, 22, 0)
>>>
在我的片段中,我减去5个月,并获得datetime.datetime(2015,9,1,22,0)。 我想摆脱小时22并获得datetime.datetime(2015,9,1,0,0)。
提前致谢。
I would like to substract months and get a result in the form yyyymmdd. To start I am given a string timestamp in sec, then here is what I'm doing:
>>> ts=1454284800
>>> billdate = datetime.datetime.strptime(time.strftime('%Y%m%d', time.gmtime(ts)), '%Y%m%d') - datetime.timedelta(5*365/12.0)
>>> billdate
datetime.datetime(2015, 9, 1, 22, 0)
>>>
In my snippet I substract 5 months, and get datetime.datetime(2015, 9, 1, 22, 0). I would like to get rid of the the hour 22 and get datetime.datetime(2015, 9, 1, 0, 0).
Thanks in advance.
原文:https://stackoverflow.com/questions/35358333
2020-03-17 12:03
满意答案
第1:22点不是白天。 这是几个小时。
第2点:如果你想摆脱时间并在12:00 AM获得日期时间,那么试试这个:
>>> billdate = datetime.datetime(billdate.year, billdate.month, billdate.day)
>>> billdate
datetime.datetime(2015, 9, 1, 0, 0)
Point 1: 22 is not day. It is hours.
Point