Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving datetime with milliseconds and time zone - python

I can't figure out the correct second argument of strptime for the following date format

a = datetime.strptime('2017-03-09 14:00:00.000000+0000', '%y-%m-%d %H:%M:%S.%f+%z')

The error I get is:

ValueError: time data '2017-03-09 14:00:00.000000+0000' does not match format '%y-%m-%d %H:%M:%S.%f+%z'

Could anyone help me out?

like image 485
Lukasz Avatar asked Mar 15 '26 21:03

Lukasz


1 Answers

The correct format string is:

%Y-%m-%d %H:%M:%S.%f%z

You can figure this out more easily next time by using strftime() to write a datetime using a given format. For example, your original:

datetime.datetime.now().astimezone(tz=None).strftime('%y-%m-%d %H:%M:%S.%f+%z')

gives:

17-03-13 22:53:50.010314++0800

And from there it is fairly easy to see what's wrong.

like image 75
John Zwinck Avatar answered Mar 17 '26 10:03

John Zwinck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!