5

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 ?

mk_sch
  • 1,060
  • 4
  • 16
  • 31
  • Possible duplicate of [How do I format a Microsoft JSON date?](http://stackoverflow.com/questions/206384/how-do-i-format-a-microsoft-json-date) –  Nov 30 '16 at 07:37
  • Possible duplicate: [Converting ASP.Net JSON Date into Python datetime](http://stackoverflow.com/questions/18039625/converting-asp-net-json-date-into-python-datetime) – salmanwahed Nov 30 '16 at 07:44
  • The out put that I asked for is different from the above questions. – mk_sch Nov 30 '16 at 08:46

3 Answers3

9

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'
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
  • @ Harsha Biyani : Thank you, the output has to be like "9:19 a.m. Oct. 28, 2016". – mk_sch Nov 30 '16 at 08:06
  • at lest that you can change the format by modifying the sequence. – Harsha Biyani Nov 30 '16 at 08:15
  • All answers pint out the same date as out put, but in the website that I am working on , it's "9:19 a.m. Oct. 28, 2016" and not " 04:43 p.m. 07. 05, 16". It's strange. – mk_sch Nov 30 '16 at 08:20
  • Thanks a lot for your answer, I executed your code for "TimestampUtc = "\/Date(1477657148000)\/" and the output is " 02:19 p.m. Oct. 28, 16", but it must be "9:19 a.m. Oct. 28, 2016". The difference is marginal but it is not exactly same :( – mk_sch Nov 30 '16 at 08:37
3

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
solideo
  • 129
  • 1
  • 13
  • @ kissbug: thank you so much, it's the closest answer to my question, just one question, The output has to be "9:19 a.m. Oct. 28, 2016" but in your code it's different. Also, I want to have 9 instead of 09. How I can do it? – mk_sch Nov 30 '16 at 08:17
  • 1
    [http://strftime.org/](http://strftime.org/) is the format of the **strftime**, and You can use **%-I** instead **%I**, different time zone run the code will get the different local time – solideo Nov 30 '16 at 08:26
  • I executed your code for "TimestampUtc = "\/Date(1477657148000)\/" and the output is "02:19 PM Oct. 28, 2016", but it must be "9:19 a.m. Oct. 28, 2016". The difference is marginal but it is not exactly same – mk_sch Nov 30 '16 at 08:32
  • It's so interesting , I am getting this times from a website from the US, how I can exactly extract the same times in that website? Also adding %-I in python 2.7 gives me an error. – mk_sch Nov 30 '16 at 08:59
0

I think the unit of your timestamp is milli seconds. So divide it into 1000 then you get 1467717221. And using date time module for get the time. Then convert it to your format by strftime.

from datetime import datetime

newTimeString = datetime.utcfromtimestamp(1467717221).strftime('%I:%M %p %b. %d, %Y')

you can find formatting from here(https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior)

Hulk
  • 271
  • 1
  • 8
  • @ Hulk: thank you , I tried this one, the correct output is "9:19 a.m. Oct. 28, 2016" but under your code it is : "11:13 AM Jul. 05, 2016" – mk_sch Nov 30 '16 at 08:07