Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109131

Re: Format a timedelta object

From Zachary Ware <zachary.ware+pylist@gmail.com>
Newsgroups comp.lang.python
Subject Re: Format a timedelta object
Date 2016-05-26 00:28 -0500
Message-ID <mailman.0.1464240542.2277.python-list@python.org> (permalink)
References <574686cc$0$1584$c3e8da3$5496439d@news.astraweb.com> <CAKJDb-Ng+o-QQAwkr7VkEXTxYnMGQhxS7VdMs6nTLg8_0fwBLQ@mail.gmail.com>

Show all headers | View raw


On Thu, May 26, 2016 at 12:16 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> I have a timedelta object, and I want to display it in a nice human-readable
> format like 03:45:17 for "three hours, forty five minutes, 17 seconds".
>
> Is there a standard way to do this?

   >>> timedelta(100)
   datetime.timedelta(100)
   >>> str(timedelta(seconds=100))
   '0:01:40'
   >>> str(timedelta(hours=100))
   '4 days, 4:00:00'

(I recently spent *way* too long trying to figure out how to properly
format the thing before being reminded that a plain str call gives
exactly what I was after.)

-- 
Zach

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Format a timedelta object Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-05-26 15:16 +1000
  Re: Format a timedelta object Zachary Ware <zachary.ware+pylist@gmail.com> - 2016-05-26 00:28 -0500
    Re: Format a timedelta object Steven D'Aprano <steve@pearwood.info> - 2016-05-28 02:21 +1000
      Re: Format a timedelta object Zachary Ware <zachary.ware+pylist@gmail.com> - 2016-05-27 11:34 -0500
      Re: Format a timedelta object Pete Forman <petef4+usenet@gmail.com> - 2016-05-27 21:41 +0100
  Re: Format a timedelta object Marko Rauhamaa <marko@pacujo.net> - 2016-05-26 08:43 +0300

csiph-web