How to convert datetime / date to milliseconds in Python?

3

How to convert a type date and a datetime to milliseconds?

>>> ontem # tipo date
datetime.date(2015, 9, 29)
>>> hoje # tipo datetime
datetime.datetime(2015, 9, 30, 18, 15, 36, 856736)
    
asked by anonymous 30.09.2015 / 23:28

3 answers

4

Build timedelta from the difference between your date and the reference date, then use total_seconds to get the total of seconds contained in the range. Since this result is expressed in floating point, you can multiply it by 1000 to get the result in milliseconds (truncated if necessary):

>>> UNIX_EPOCH = datetime.datetime(1970, 1, 1)
>>> data = datetime.datetime(2015, 9, 30, 18, 15, 36, 856736)
>>> (data - UNIX_EPOCH).total_seconds()*1000
1443636936856.7358
>>> int((data - UNIX_EPOCH).total_seconds()*1000)
1443636936856

Font

If your date is timezone-aware , remember to use the same time zone on the reference date and on the date you want to represent in milliseconds (or rather convert your date to UTC before to do this representation, this is all canonical). If everything is in UTC, you can also get the value of "Unix Season" by:

UNIX_EPOCH = datetime.datetime.utcfromtimestamp(0)

( utcfromtimestamp expects a value in seconds ), and can reject values outside of the 1970-2038 range, depending on the implementation; in my opinion, it is safer to declare the constant explicitly without dependency of this method)

Note: It does not make sense to represent a date in milliseconds, since a date is a whole day. I suggest choosing [consistently] an instant on that date (for example midnight) and creating a datetime from your date .

    
30.09.2015 / 23:45
2

You can convert in several ways:

Solution 1

Using datetime.timestamp() present in Python 3.

>>> import datetime
>>> hoje = datetime.datetime(2015, 9, 30)
>>> int(hoje.timestamp() * 1000)
1443582000000
  

Note that timestamp() only works with datetime and not date

Then you need to convert to datetime if you are working with date :

>>> hoje = datetime.date(2015, 9, 30)
>>> int(datetime.datetime(*hoje.timetuple()[:3]).timestamp() * 1000)
1443582000000
Solution 2

Using strftime() .

>>> import datetime
>>> hoje = datetime.date(2015, 9, 30)
>>> int(hoje.strftime('%s')) * 1000
1441681200000
  

See that it is %s with s and not uppercase. Using s will return only the seconds of the date (in this case 00 ) and not the total seconds since 1970.

Comments:

  

I did not find a reference to s in the documentation, but it works in Python 3 and 2 in Mac OS.

     

The strftime('%s') returns only the total rounded seconds, preventing accuracy in milliseconds.

Solution 3

Using mktime() of module time :

>>> import datetime, time
>>> hoje = datetime.date(2015, 9, 30)
>>> int(time.mktime(hoje.timetuple()) * 1000)
1443582000000
  

Note that mktime returns only the rounded total seconds, not allowing precision for milliseconds.

Solution 4

Using total_seconds() of timedelta :

When you make the difference between two datetime a timedelta is returned and from it you can return the number of total seconds using total_seconds() .

>>> import datetime
>>> hoje = datetime.datetime(2015, 9, 30)
>>> (hoje - datetime.datetime.utcfromtimestamp(0)).total_seconds() * 1000
1443582000000
  

The utcfromtimestamp(0) returns a datetime.datetime(1970, 1, 1, 0, 0) as mentioned by mgibsonbr.

    
01.10.2015 / 00:35
1

Given that the datetime object already has a method for representing a date in seconds since January 1, 1970, the following is enough:

data = datetime.datetime.now()
segundos = data.timestamp()
milissegundos = segundos * 1000

If you have a date object, convert it to datetime:

d = datetime.date(2015, 5, 5)    
data = datetime.datetime.fromordinal(d.toordinal())
    
01.10.2015 / 00:28