1

I have to read a JSON file and It has the date fields formated like this below

"MyDate": "/Date(1603284014088)/",

I found some examples to parse it in other languages (JavaScript, Kotlin) but nothing in Python.

I could find on this SCOTT HANSELMAN's post here that they are milliseconds since the beginning of the Unix Epoch sometimes with a TimeZone. So I could write a simple function to decode it.

import re
from datetime import datetime, timedelta

def decode_date (encoded_date):
    mask = "\/Date\(([0-9]*)\)"
    offset = datetime(1970, 1, 1)
    my_matchs = re.match(mask, encoded_date).groups()
    if len(my_matchs) == 1:
        return datetime(1970, 1, 1) + timedelta(milliseconds=int(my_matchs[0]))
    else:
        return None
    

encoded = "/Date(1603284014088)/"
print (decode_date(encoded))

This function can't parse dates with timezone because I'm lazy :), My question is - is there some lib that can parse it out of the box in Python?

5
  • 2
    You don't - you tell whoever created that service that eve ASP.NET Web API uses ISO8601 for dates. Nobody uses that Unix timestamp for almost 10 years Commented Oct 23, 2020 at 14:45
  • You can use datetime.fromtimestamp to convert the Unix timestamp value directly, there's no need to do it yourself Commented Oct 23, 2020 at 14:50
  • 1
    @PanagiotisKanavos I could live with milliseconds-since-the-epoch being returned. What really isn't necessary is the /Date(...) wrapper around it. Commented Oct 23, 2020 at 15:00
  • I edited my code to deal with not match strings.. I absolutellly agree with Panagiotis Kanavos but it was a ended project already validated I can ask that guy to change it now. Commented Oct 23, 2020 at 15:04
  • Hello, did you have a moment to check my reply? If it was useful for you please consider upvoting it and / or choosing it as final answer. Thanks for your time! Commented Mar 26, 2021 at 9:20

1 Answer 1

1

Given the fact that you are already able to extract the date using RegEx please try the following code for conversion:

import datetime

s = '1603284014088'
fmt = "%Y-%m-%d %H:%M:%S"

# Local time
t = datetime.datetime.fromtimestamp(float(s)/1000.)
print (t.strftime(fmt))

# UTC time
t_utc = datetime.datetime.utcfromtimestamp(float(s)/1000.)
print (t_utc.strftime(fmt))
Sign up to request clarification or add additional context in comments.

4 Comments

The call to float isn't necessary. Dividing by a float will produce a float result. In Python 3, / will always produce a float, even if both arguments are ints, so s/1000 is sufficient.
@chepner s is a string so you need the call to float.
Oh, sorry, got ahead of myself. I assumed s was already an int.
But that said, int(s)/1000 would be sufficient and "stricter", as it is expected that s is the string representation of an int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.