Round timestamp to nearest day in Python

前端 未结 3 869
天命终不由人
天命终不由人 2021-01-18 03:14

In Python 2.7.2 I am getting the seconds since epoch using:

sec_since_epoch = (date_obj - datetime(1970, 1, 1, 0, 0)).total_seconds()

Now I want

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 03:35

    You can use datetime.timetuple() to manipulate with the date. E.g. in this way:

    from datetime import datetime
    
    
    dt = datetime(2013, 12, 14, 5, 0, 0)
    dt = datetime(*dt.timetuple()[:3]) # 2013-12-14 00:00:00
    print dt.strftime('%s') # 1386997200
    

    DEMO

提交回复
热议问题