Skip to content Skip to sidebar Skip to footer

Datetime: Print As Seconds

I have a datetime object. I want to print it as just number of seconds (i.e., 1 min 30.5 sec should print as 90.5 s). Can't seem to find a way to do it with strftime.

Solution 1:

I think that for your situation you'd be better off using a datetime.timedelta object. It has a function that will do exactly what you want, datetime.timedelta.total_seconds().


Solution 2:

Datetimes are instants in time, unlike durations which are just numbers. The reason strftime won't say "90.5s" is that, well, that is a duration and not a datetime.

Does your datetime object represent the instant that is 90.5 seconds past the epoch? If so, just get the epoch time in millis and divide by 1000, concat an "s" and you are good to go.

Also, for fun, see http://en.wikipedia.org/wiki/ISO_8601#Durations.


Post a Comment for "Datetime: Print As Seconds"