Removing “ x days ” from datetime.timedelta object

后端 未结 1 1953
星月不相逢
星月不相逢 2020-12-21 18:49

I need to create a report where I need to subtract two dates and return it in the form of %H:%M%S

This is the subtraction which I inserted into a list :



        
相关标签:
1条回答
  • 2020-12-21 19:51

    you can use a custom function that converts timedelta to H:M:S string:

    def td_to_str(td):
        """
        convert a timedelta object td to a string in HH:MM:SS format.
        """
        hours, remainder = divmod(td.total_seconds(), 3600)
        minutes, seconds = divmod(remainder, 60)
        return f'{int(hours):02}:{int(minutes):02}:{int(seconds):02}'
    
    s = pd.Series(pd.to_timedelta(['1 day, 00:05:39']))
    
    s.apply(td_to_str)
    # 0    24:05:39
    # dtype: object
    
    • ref
    0 讨论(0)
提交回复
热议问题