Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time format in python

I have a date that looks like this :

November 19th 2015, 12:48:34.402

And I need to figure out a way to convert it to a timestamp. I'm trying it this way but I can't find the right pattern to format it.

time.mktime(datetime.datetime.strptime(parsed["_timestamp"], "%B %d %Y, %H:%M:%S").timetuple())

Any suggestion on maybe how to get the right format?

like image 382
JonathanG Avatar asked May 11 '26 17:05

JonathanG


1 Answers

Your formatting string was a bit off. You need to include the th, rd, etc., the decimal point for the seconds, and the microseconds (which is %f). We'll find the proper th/etc. with a split():

>>> a = 'November 19th 2015, 12:48:34.402'
>>> time.mktime(datetime.datetime.strptime(a, "%B %d{} %Y, %H:%M:%S.%f".format(a.split()[1][-2:])).timetuple())
1447966114.0
>>> a = 'November 23rd 2015, 12:48:34.402'
>>> time.mktime(datetime.datetime.strptime(a, "%B %d{} %Y, %H:%M:%S.%f".format(a.split()[1][-2:])).timetuple())
1448311714.0
like image 195
TigerhawkT3 Avatar answered May 14 '26 08:05

TigerhawkT3



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!