mk_sch mk_sch - 7 days ago 5
JSON Question

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

I am parsing a JSON file that includes

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


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

9:19 a.m. Oct. 28, 2016


But this format is not that common and other similar questions don't answer it. IS there any idea how to do it ?

Answer

This:

import datetime
import re


TimestampUtc = "\/Date(1467717221000)\/"

TimestampUtc = re.split('\(|\)', TimestampUtc)[1][:10]
date = datetime.datetime.fromtimestamp(int(TimestampUtc))
print date
print date.strftime('%I:%M %p %b. %d, %Y')
print date.strftime('%I:%M %p %b. %d, %Y').replace('AM', 'a.m.').replace('PM', 'p.m.')

Output:

2016-07-05 19:13:41
07:13 PM Jul. 05, 2016
07:13 p.m. Jul. 05, 2016