Convert strange Python date format to readable date

I use Python to access the mobile API of some web service, and the answer contains the following strange date notation: u'/Date(1409522400000+0200)/' This should be September 1, 2014.

I'm not sure which format it is, but I would like to convert it to something readable, i.e. date or datetime or Unix time.

Can anyone help me with this?

+6
source share
2 answers

The timeline looks like OData version 2 JSON is a detailed format for Datetime, which can be seen in older ASP.NET or WCF applications :

"/ Date (<ticks> [" + "|" - "<offset]] /"
<ticks> = number of milliseconds since midnight January 1, 1970
<offset> = utc offset

 #!/usr/bin/env python3 import re from datetime import datetime, timedelta, timezone time_string = u"/Date(1409522400000+0200)/" epoch = datetime(1970, 1, 1, tzinfo=timezone.utc) ticks, offset = re.match(r'/Date\((\d+)([+-]\d{4})?\)/$', time_string).groups() utc_dt = epoch + timedelta(milliseconds=int(ticks)) print(utc_dt) if offset: offset = int(offset) hours, minutes = divmod(abs(offset), 100) if offset < 0: hours, minutes = -hours, -minutes dt = utc_dt.astimezone(timezone(timedelta(hours=hours, minutes=minutes))) print(dt) 

Output

 2014-08-31 22:00:00+00:00 2014-09-01 00:00:00+02:00 

where timezone is defined here .

+6
source

did you get the timestamp (java?) in milliseconds. you can convert it to something more readable as follows:

 from datetime import date d=1409522400000/1000.0 # divide by 1000 to get seconds print date.fromtimestamp(d) # -> 2014-09-01 
+4
source

Source: https://habr.com/ru/post/982390/


All Articles