Skip to content Skip to sidebar Skip to footer

Changing The Format Of Timestamp In Python

I am trying to change the timestamp from one format to another. I tried some approaches but i am not able to succeed. This is my timestamp 151005-12:07:34.917928 I would like to ch

Solution 1:

Here's one way using regular expressions:

import re
e = '(\d{2})(\d{2})(\d{2})-(\d{2}:\d{2}:\d{2})\.(\d+)'
s = '151005-12:07:34.917928'
print('20{}-{}-{} {}:{}'.format(*re.search(e, s).groups()))

2015-10-05 12:07:34:917928

Here's another using the datetime module:

import datetime
e = '%y%m%d-%H:%M:%S.%f'
d = datetime.datetime.strptime(s, e)
print(datetime.datetime.strftime(d, '%Y-%m-%d %H:%M:%S:%f'))

2015-10-05 12:07:34:917928

Post a Comment for "Changing The Format Of Timestamp In Python"