janecs
janecs

Reputation: 209

print part of a timestamp using python

I am outputting timestamps (eg. 2012-02-24 13:18:45.552882) and I'd like to remove the last part (here would be .552882) from the output file, so I would only have date and time to the second. How can I do this?

Thanks in advance!

Upvotes: 1

Views: 1002

Answers (3)

Fred
Fred

Reputation: 1021

>>> date = '2012-02-24 13:18:45.552882'
>>> good, bad = date.split('.')
>>> good
'2012-02-24 13:18:45'
>>> bad
'552882'

Upvotes: 1

Akash
Akash

Reputation: 295

You can use datetime.strftime(format). See this

Upvotes: 1

user130076
user130076

Reputation:

Have a look at the strftime method in datetime.

Upvotes: 4

Related Questions