-1

My Json File 'DueDate': '/Date(1539205200000)/', 'Bydate': '/Date(-62135578800000)/', 'NeedsAppointment': False

Code:

for o in odpg:
    try:
        duedate= o['DueDate']
        print(duedate)
    except:
        # Handle error

Am getting output as:

/Date(1539205200000)/

Required Output:

07/31/2018 
pwinz
  • 303
  • 2
  • 14
Craze
  • 1
  • 4
  • Convert unixtimstamp to datetime using `date=datetime.datetime.fromtimestamp(eval(task_datetime)).strftime('%Y-%m-%d')` Jus by importing `import datetime` – Rohit-Pandey Oct 07 '18 at 05:42
  • Am getting invalid syntax (, line 1) – Craze Oct 07 '18 at 05:45
  • What _are_ `DueDate` and `ByDate` exactly… the numbers are too big for an epoch and the second number there is negative? Are you sure that your JSON is correct? – Jens Oct 07 '18 at 06:04

2 Answers2

0

Are you sure this is the correct number? At first I thought it’s an epoch but that resulted in much too big a year; however, taking away the last three 0s works which makes me wonder if the epoch is in milliseconds instead of seconds (wild hand-wavy assumption):

>>> import time
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1539205200000))
'50745-06-22 00:00:00'
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1539205200))
'2018-10-10 21:00:00'

or

>>> import datetime
>>> datetime.datetime.fromtimestamp(1539205200000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: year is out of range
>>> datetime.datetime.fromtimestamp(1539205200)
datetime.datetime(2018, 10, 11, 7, 0)

To produce your desired output, just change the format string:

>>> # Assuming constant format "/Date(…)/", this also removes the trailing zeroes..
>>> funky_timestamp = int(o["duedate"][6:-5])
>>> datetime.datetime.fromtimestamp(funky_timestamp).strftime("%d/%m/%Y")
'11/10/2018'
Jens
  • 8,423
  • 9
  • 58
  • 78
  • exception here an integer is required (got type str) – Craze Oct 07 '18 at 05:51
  • Then use [`int()`](https://docs.python.org/3/library/functions.html#int) to convert the string `"1539205200000"` to an integer. But beware the three zeroes and my question, _Are you sure this is the correct number?_ – Jens Oct 07 '18 at 05:53
  • Am getting the value as **/Date(1539205200000)/** from json – Craze Oct 07 '18 at 05:56
  • I’ve updated my answer: assuming the `o["duedate"]` string is _*always*_ formatted the same way, you can extract the [substring using Python’s slice notation](https://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python). – Jens Oct 07 '18 at 05:59
  • @Craze, if this solves your problem then please accept the answer. But beware of my initial question: are you sure that these numbers are [Unix epoch](https://en.wikipedia.org/wiki/Unix_time) values? – Jens Oct 07 '18 at 06:15
0
import re
import datetime
json = {'DueDate': '/Date(1539205200000)/', 'Bydate': '/Date(-62135578800000)/', 'NeedsAppointment': False}
duedate = re.findall(r'[\d]+', json['DueDate'])
datetime.datetime.fromtimestamp(float(duedate[0][:10])).strftime("%d/%m/%Y")
Rohit-Pandey
  • 2,039
  • 17
  • 24