Python convert timedeltas into hours in string format -
i have timedelta object 3457 hours
timedelta(hours=3457)
i want represent in "hh:mm" format, "3457:00"
i do:
from datetime import datetime hours = timedelta(hours=3457) hours_string = time.strftime("%h:%m", time.gmtime(hours.seconds)) print hours_string "01:00"
how can "3457:00"?
please note 3457:00 nonsensical format. "hour-colon-minutes" format used in dates , times, , hour can't reasonably higher 23. more reasonable format is: 3457h 0m.
you can this:
from datetime import timedelta delta = timedelta(hours=3457) minutes, seconds = divmod(delta.seconds, 60) hours, minutes = divmod(minutes, 60) hours += delta.days * 24 print '%sh %sm' % (hours, minutes)
of course, easier way this:
from datetime import timedelta delta = timedelta(hours=3457) print delta
but give "144 days, 1:00:00", sane format, not want.
Comments
Post a Comment