3

How do I parse a time like this '/Date(-62135596800000)/' with Python?

I searched the internet, but was unable to find a solution.

Link:http://211.137.19.74:8089/Ajax/GetStationDetailList?cityName=%E6%B2%88%E9%98%B3

TimePoint:"/Date(-62135596800000)/" and the time should be 2016:12:31: 01:00

Tony Wang
  • 971
  • 4
  • 16
  • 33
  • 1
    What date is that number supposed to represent? Probably not "62 trillion seconds before the Unix epoch", I'm guessing. – Kevin Dec 30 '16 at 16:50
  • @Kevin... that would be normal for paleontologists! – tdelaney Dec 30 '16 at 16:54
  • Can you give us some context on where this date string came from? – tdelaney Dec 30 '16 at 16:54
  • @tdelaney added some detail – Tony Wang Dec 30 '16 at 17:44
  • @Kevin I've added some detail – Tony Wang Dec 30 '16 at 17:44
  • 2
    It's an ASP.NET JSON Date (old-style), and that value is equivalent to `0001-01-01T00:00:00.000Z` (Which is `DateTime.MinValue` in UTC in .NET). The number is *milliseconds* since the Unix epoch. – Matt Johnson-Pint Dec 30 '16 at 17:51
  • Are you certain that the string you've given `Date(-62135596800000)` actually equates to 2016:12:31: 01:00? What would be the number for 2016:12:31: 02:00? – Bryan Oakley Dec 30 '16 at 18:11
  • @BryanOakley you can check the home page here. The datetime shows in the right up corner . http://211.137.19.74:8089/ – Tony Wang Dec 31 '16 at 11:19
  • @MattJohnson So the time isn't the right time? – Tony Wang Dec 31 '16 at 11:25
  • 1
    That's right. All values in your API responses are set to the default date of `0001-01-01 00:00:00.000 UTC`. For whatever reason, the server is not sending any current dates on that field. – Matt Johnson-Pint Jan 01 '17 at 02:11
  • @MattJohnson Got it. Though I didn't know what's the meaning of this time format. But It remains the same and nothing changed. So I have to find another way to get the current date. Thanks very much! – Tony Wang Jan 01 '17 at 05:53

3 Answers3

3

/Date(N)/ is usually the format for dates serialized by the built-in WCF JSON serializer in .NET. The value can then be reconstructed by calling new Date(N) in JavaScript.

Running that in JavaScript will show why this date is very likely just wrong:

new Date(-62135596800000)
// Date 0001-01-01T00:00:00.000Z

As you can see, you get the 0001-01-01 00:00:00.00. This also happens to be the default value in .NET for empty DateTime objects:

default(DateTime).ToString("u") // 0001-01-01 00:00:00Z
new DateTime(0).ToString("u") // 0001-01-01 00:00:00Z

So, sorry to say it like that, but that server is just broken.


That all being said, once you get a valid date, you can parse the number in Python using datetime.fromtimestamp or datetime.utcfromtimestamp:

>>> from datetime import datetime
>>> datetime.fromtimestamp(1483120713887 // 1000)
datetime.datetime(2016, 12, 30, 18, 58, 33)
poke
  • 369,085
  • 72
  • 557
  • 602
1

Using python 3.x
Looks like epoch time (in nano seconds starting January 1, 1970 00:00:00.000 GMT). You could use something like this:

from datetime import datetime
dt = datetime.fromtimestamp(62135596800000 // 1000000000)
print(dt)

As for the value you're getting, you can't have a negative epoch time stamp (-62135596800000 would be invalid). If the - indicates negative, it might be another time stamp format.

ConorSheehan1
  • 1,640
  • 1
  • 18
  • 30
  • 1
    Milliseconds, not nanoseconds, and yes - you absolutely can have a negative epoch timestamp. The unix epoch is `1970-01-01T00:00:00Z`. You'll get negatives in 1969 and earlier. – Matt Johnson-Pint Dec 30 '16 at 17:52
  • 1
    @MattJohnson Oh sorry, I didn't realize that. The datetime.fromtimestamp function throws an error when I used the negative value. Turns out python restricts the date range by default for some reason. "It’s common for this to be restricted to years in 1970 through 2038" - from the [docs](https://docs.python.org/3/library/datetime.html) – ConorSheehan1 Dec 30 '16 at 18:32
  • 1
    Interesting... And [here](http://stackoverflow.com/a/17231712/634824) is a solution to that. ;) – Matt Johnson-Pint Dec 30 '16 at 18:34
0

Source: ASP.NET FORUMS

var dateString = "/Date(1526409000000)/".substr(6);
var currentTime = new Date(parseInt(dateString));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = day + "/" + month + "/" + year;
alert(date);
Pranesh Janarthanan
  • 1,134
  • 17
  • 26