235

I have a data file containing timestamps like "1331856000000". Unfortunately, I don't have a lot of documentation for the format, so I'm not sure how the timestamp is formatted. I've tried Python's standard datetime.fromordinal() and datetime.fromtimestamp() and a few others, but nothing matches. I'm pretty sure that particular number corresponds to the current date (e.g. 2012-3-16), but not much more.

How do I convert this number to a datetime?

fragilewindows
  • 1,354
  • 1
  • 14
  • 26
Cerin
  • 55,951
  • 85
  • 291
  • 487

3 Answers3

429

datetime.datetime.fromtimestamp() is correct, except you are probably having timestamp in miliseconds (like in JavaScript), but fromtimestamp() expects Unix timestamp, in seconds.

Do it like that:

>>> import datetime
>>> your_timestamp = 1331856000000
>>> date = datetime.datetime.fromtimestamp(your_timestamp / 1e3)

and the result is:

>>> date
datetime.datetime(2012, 3, 16, 1, 0)

Does it answer your question?

EDIT: J.F. Sebastian correctly suggested to use true division by 1e3 (float 1000). The difference is significant, if you would like to get precise results, thus I changed my answer. The difference results from the default behaviour of Python 2.x, which always returns int when dividing (using / operator) int by int (this is called floor division). By replacing the divisor 1000 (being an int) with the 1e3 divisor (being representation of 1000 as float) or with float(1000) (or 1000. etc.), the division becomes true division. Python 2.x returns float when dividing int by float, float by int, float by float etc. And when there is some fractional part in the timestamp passed to fromtimestamp() method, this method's result also contains information about that fractional part (as the number of microseconds).

maxkoryukov
  • 3,623
  • 5
  • 28
  • 48
Tadeck
  • 125,377
  • 26
  • 148
  • 197
  • 18
    use true division: `/ 1e3` – jfs Mar 16 '12 at 21:42
  • @J.F.Sebastian: You are totally correct, I have edited my answer and added some explanation why you are right. If you do not agree with the explanation, please let me know. – Tadeck Mar 16 '12 at 22:06
  • @manu-fatto: That does not really matter. `x * 0.001` and `x / 1e3` are both the same, the difference is in the notation and length (the original one is shorter). For some people it may be clearer to actually divide by the number instead of multiplying by the _multiplicative inverse_ number (`1/x`), which you proposed. But thanks for proposing alternative approach. – Tadeck Nov 06 '13 at 23:42
  • 71
    This function is dangerous, because takes you timezone into consideration. Try looking up utcfromtimestamp – Haim Bender Jan 02 '14 at 09:38
  • Thanks! This also fixes the error "ValueError: year is out of range" – douglaslps Aug 18 '14 at 13:25
  • @EmanuelePaolini: x / 1e3 is easier to parse than x * 0.001 as it's easy to make a mistake counting zeros. x * 1e-3 is just as easy to parse (although I prefer personally x / 1e3) – Charles Plager Apr 06 '16 at 19:10
  • 17
    @HaimBender Thanks! I feel like your comments deserve more attentions. `fromtimestamp` give you the date and time in local time `utcfromtimestamp` gives you the date and time in UTC. – yusong Dec 04 '16 at 17:49
  • The first attempt of `float`ing the file's `stat` access time (in Python 2.7.12) failed when dividing by `/ 1e3`. Dropping the `/ 1e3` gave perfect results. – WinEunuuchs2Unix Jul 22 '20 at 23:22
  • @Tadeck Finally! Thank you. I didn't realize my database timestamp was in miliseconds and datatime couldn't use it. Spent hours on this until I saw your answer. / 1e3 fixed it – Dowlers Sep 18 '20 at 16:40
4

Alternatively, you can use pandas.to_datetime and choose the units for yourself together with the timezone. That avoids all the comments and problems mentioned in the previous answer:

import pandas as pd

pd.to_datetime(int('1331856000000'), utc=True, unit='ms')
# Timestamp('2012-03-16 00:00:00+0000', tz='UTC')
My Work
  • 1,588
  • 1
  • 11
  • 39
0

Similar to @Tadek answer

If the timestamps are in UTC timezone (a common way to store dates) you should use

datetime.datetime.utcfromtimestamp()

If you use fromtimestamp it will assume the date is represented in your local timezone

James Wierzba
  • 14,715
  • 12
  • 69
  • 110