changing format of time duration.

Discussion in 'Python' started by dave, Jun 3, 2010.

  1. dave

    dave Guest

    Quick question. I have to time stamps (now and now2).

    now = datetime.datetime.now();
    now2 = datetime.datetime.now();

    now2-now1 yields me a result in 0:00:00.11221 (H:MM:SS.ssss)

    I wanted to know if there is a standard python method or a quick hack
    to add an extra zero in the beginning.

    So the result I am looking for would be 00:00:00.11221
     
    dave, Jun 3, 2010
    #1
    1. Advertisements

  2. On 3 jun, 17:24, dave <> wrote:
    > Quick question. I have to time stamps (now and now2).
    >
    > now = datetime.datetime.now();
    > now2 = datetime.datetime.now();
    >
    > now2-now1 yields me a result in 0:00:00.11221 (H:MM:SS.ssss)
    >
    > I wanted to know if there is a standard python method or a quick hack
    > to add an extra zero in the beginning.
    >
    > So the result I am looking for would be 00:00:00.11221


    Try the strptime method with a suitable format, like this (untested):
    delta = now2-now1
    delta.strftime('%H:%M:%S.%f')

    http://docs.python.org/library/datetime.html#strftime-strptime-behavior

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jun 4, 2010
    #2
    1. Advertisements

  3. Gabriel Genellina <> wrote:

    >Try the strptime method with a suitable format, like this (untested):
    >delta = now2-now1
    >delta.strftime('%H:%M:%S.%f')


    Throws an exception:

    |Traceback (most recent call last):
    | File "<stdin>", line 1, in <module>
    |AttributeError: 'datetime.timedelta' object has no attribute 'strftime'

    What seems logical, since the documentation doesn't mention an strftime
    method for timedelta.



    Regards,

    Günther
     
    Günther Dietrich, Jun 4, 2010
    #3
  4. On 4 jun, 06:14, "Günther Dietrich" <> wrote:
    > GabrielGenellina<> wrote:


    > >Try the strptime method with a suitable format, like this (untested):
    > >delta = now2-now1
    > >delta.strftime('%H:%M:%S.%f')

    >
    > Throws an exception:
    >
    > |Traceback (most recent call last):
    > |  File "<stdin>", line 1, in <module>
    > |AttributeError: 'datetime.timedelta' object has no attribute 'strftime'
    >
    > What seems logical, since the documentation doesn't mention an strftime
    > method for timedelta.


    You're right. Second try (still untested):

    def nice_timedelta_str(d):
    result = str(d)
    if result[1] == ':':
    result = '0' + result
    return result

    delta = now2-now1
    print nice_timedelta_str(delta)


    --
    Gabriel Genellina
     
    Gabriel Genellina, Jun 5, 2010
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shaiboy_UK
    Replies:
    0
    Views:
    570
    Shaiboy_UK
    Oct 19, 2003
  2. CJ
    Replies:
    2
    Views:
    645
  3. Replies:
    9
    Views:
    446
  4. rainny

    Count the time duration

    rainny, Mar 23, 2009, in forum: Java
    Replies:
    13
    Views:
    2,739
  5. Shaiboy_UK

    Time/Date format and changing time to GMT

    Shaiboy_UK, Oct 19, 2003, in forum: ASP General
    Replies:
    1
    Views:
    251
    Bob Barrows
    Oct 19, 2003
Loading...

Share This Page