How to remove seconds from datetime?

前端 未结 10 1910
终归单人心
终归单人心 2021-02-20 13:02

I have the following date and I tried the following code,

df[\'start_date_time\'] = [\"2016-05-19 08:25:00\",\"2016-05-19 16:00:00\",\"2016-05-20 07:45:00\",\"20         


        
10条回答
  •  误落风尘
    2021-02-20 14:02

    Convert the string to a datetime object and then manipulate that

    >>> x = ["2016-05-19 08:25:00","2016-05-19 16:00:00","2016-05-20 07:45:00","2016-05-24 12:50:00","2016-05-25 23:00:00","2016-05-26 19:45:00"]
    >>> for i in x:
    ...  y = datetime.datetime.strptime(i, '%Y-%m-%d %H:%M:%S')
    ...  z = datetime.datetime.strftime(y, '%Y-%m-%d %H:%M')
    ...  print (y, type(y))
    ...  print (z, type(z))
    ... 
    (datetime.datetime(2016, 5, 19, 8, 25), )
    ('2016-05-19 08:25', )
    (datetime.datetime(2016, 5, 19, 16, 0), )
    ('2016-05-19 16:00', )
    (datetime.datetime(2016, 5, 20, 7, 45), )
    ('2016-05-20 07:45', )
    (datetime.datetime(2016, 5, 24, 12, 50), )
    ('2016-05-24 12:50', )
    (datetime.datetime(2016, 5, 25, 23, 0), )
    ('2016-05-25 23:00', )
    (datetime.datetime(2016, 5, 26, 19, 45), )
    ('2016-05-26 19:45', )
    

提交回复
热议问题