2

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 change it to a format like this 2015-10-05 12:07:34:917928

Thanks in Advance

1
  • 2
    Show your code that you tried. Commented Oct 6, 2015 at 11:47

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

note: the datetime approach validates the values and therefore it should be used if the input data may be invalid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.