0

Recently I received this output from an API (I think it's .NET driven).. What kind of date format is this and how do I convert it to a Python date object?

{
    Id: 10900001,
    ExpirationDate: "/Date(1262476800000)/",
}

It seems a timestamp but I get parse errors on fromtimestamp()

>>> from datetime import datetime
>>> datetime.fromtimestamp(float('1262476800000'))
ValueError: 'year is out of range'
Bea Trix
  • 75
  • 1
  • 7

6 Answers6

4

You are getting the error because the timestamp is in milliseconds. You just remove the last 3 digits and it will work -

>>> from datetime import datetime, timedelta
>>> s = '1262476800540'
>>> d = datetime.fromtimestamp(float(s[:-3]))
>>> d = d + timedelta(milliseconds=int(s[-3:]))
>>> print d
2010-01-03 05:30:00.540000
Vikas Ojha
  • 6,742
  • 6
  • 22
  • 35
2
>>> from datetime import datetime
>>> a = datetime.fromtimestamp(float('1262476800002')/1000)
>>> a.microsecond
2000
luoluo
  • 5,353
  • 3
  • 30
  • 41
1

As it seems the API's output is JSON, I would assume it is a javascript timestamp. To convert it to python, remove the milliseconds and you should be fine.

From an online conversion tool http://www.epochconverter.com/

GMT: Sun, 03 Jan 2010 00:00:00 GMT
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
0

It seems like a UNIX time stamp in milliseconds. In other words 1262476800(000) = 2010-01-03T00:00:00+00:00

Could that be correct?

You can use fromtimestamp to convert it to a date object.

Cheers, Anders

Anders Borg
  • 72
  • 10
  • I ruled that out, because I was expecting an expiration date to be in the future, not five years ago. – nigel222 Sep 18 '15 at 09:27
  • They might have used some other base time when creating the timestamp. In C# you could do something like this. Maybe they used something other than 1970. static DateTime ConvertFromUnixTimestamp(double timestamp) { DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); return origin.AddSeconds(timestamp); } In any case, it's clear from the value that the timestamp contains only date and not time. – Anders Borg Sep 18 '15 at 13:49
0

Also you can use time module.

In [13]: import time

In [14]: time.ctime(1262476800000)
Out[14]: 'Mon Apr 19 02:00:00 41976\n'
wolendranh
  • 4,202
  • 1
  • 28
  • 37
0

I think you'll have to peruse the documentation of that API. Failing which, can you reverse-engineer it? If you can create entities in that system, then do so. If you create 3 entities expiring on 1st Jan 2016, 11th Jan 2016 and 21st Jan 2016, you'll be able to see if it's (probably) a linearly increasing sequence of time-units and deduce what are the units and the base-date. Even if you can't create entities, can you get the dates in human-readable format through a human-orientated web interface?

Once you know what the number represents, you can decode it either into year, month, day ... fields, or into a number of seconds since the canonical base date (timestamp).

Or maybe you'll get lucky and another reader will recognise the format!

nigel222
  • 7,582
  • 1
  • 14
  • 22
  • Also can you establish what OS and/or database is storing the data? Native date/time representation for the system, available date/time field formats for the DB, might help you. – nigel222 Sep 18 '15 at 09:30