python substract_python减去datetime对象的月份(python substract months from datetime object)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值