1

How to deserialize with Python a JSON date exported with DataContractJsonSerializer?

The dates exported look like /Date(1440051107000-0500)/ and I need to create real DateTime objects in Python.

Is there an already existing code that does this, or I do have to code it myself?

sorin
  • 161,544
  • 178
  • 535
  • 806

1 Answers1

0

I think you'll find the answer in this thread: Converting ASP.Net JSON Date into Python datetime

Hope it helps.

EDIT: Following @SuperBiasedMan advice, here is the answer provided by the link.

Apparently there is no code in python's standard library to cast this string in a datetime object.

However, this code will do the trick:

#!/usr/bin/env python

import datetime

EPOCH = datetime.datetime.utcfromtimestamp(0)

def parse_date(datestring):
    timepart = datestring.split('(')[1].split(')')[0]
    milliseconds = int(timepart[:-5])
    hours = int(timepart[-5:]) / 100
    adjustedseconds = milliseconds / 1000 + hours * 3600

    return EPOCH + datetime.timedelta(seconds=adjustedseconds)

ScheduleDate = "\/Date(1374811200000-0400)\/"
StartTime = "\/Date(-2208931200000-0500)\/"

print(parse_date(ScheduleDate))
print(parse_date(StartTime))
Community
  • 1
  • 1
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – SuperBiasedMan Aug 20 '15 at 14:06