Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert JSON date to the a special format in Python?

I am parsing a JSON file in Python 2.7 that includes

"TimestampUtc":"\/Date(1477393888000)\/

and I want to parse this file and convert the date into :

8:11 a.m. Oct. 25, 2016

The original time zone is in the US and I want to get exactly the same output. But this format is not that common and other similar questions don't answer it. Is there any idea how to do it ?

like image 338
mk_sch Avatar asked Jan 06 '23 00:01

mk_sch


1 Answers

you can try:

>>> from datetime import datetime
>>> d = "1467717221000"
>>> d = int(d[:10])
>>> datetime.fromtimestamp(d).strftime('%Y-%m-%d %I:%M:%S %p')
'2016-07-05 04:43:41 PM'

edit: Format update :

>>> datetime.fromtimestamp(d).strftime(' %I:%M %p %b. %d, %y').replace("PM","p.m.").replace("AM","a.m.")
' 04:43 p.m. 07. 05, 16'
like image 180
Harsha Biyani Avatar answered Jan 13 '23 13:01

Harsha Biyani