max
max

Reputation: 52353

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.

Upvotes: 3

Views: 1172

Answers (2)

Vorticity
Vorticity

Reputation: 4936

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().

Upvotes: 12

Ray Toal
Ray Toal

Reputation: 88468

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.

Upvotes: 0

Related Questions