3
How to convert a type date
and a datetime
millisecond?
>>> ontem # tipo date
datetime.date(2015, 9, 29)
>>> hoje # tipo datetime
datetime.datetime(2015, 9, 30, 18, 15, 36, 856736)
3
How to convert a type date
and a datetime
millisecond?
>>> ontem # tipo date
datetime.date(2015, 9, 29)
>>> hoje # tipo datetime
datetime.datetime(2015, 9, 30, 18, 15, 36, 856736)
4
Build a timedelta
from the difference between your date and the reference date, then use total_seconds
to get the total of seconds contained in the interval. As this result comes expressed in floating point, you can multiply it by 1000
to obtain the result in milliseconds (truncating 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
If your date is Timezone-Aware, remember to use the same time zone on the reference date and the date you want to represent in milliseconds (or rather, convert your date to UTC before making this representation, so it’s all canonical). If everything is in UTC, you can also get the value of "Unix Epoch" by:
UNIX_EPOCH = datetime.datetime.utcfromtimestamp(0)
(the utcfromtimestamp
expects a value in seconds, and may reject values outside the 1970-2038 range, depending on implementation; in my opinion, it is safer to state the constant explicitly, without depending on this method)
Note: it makes no sense to represent a date per millisecond, since a date corresponds to a full day. I suggest choosing [consistently] an instant on that date (for example midnight) and creating a datetime
from their date
.
In case the system passes only the date, before returning the milliseconds is added 0 hours and 0 minutes.
3
It is possible to convert in several ways:
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 withdatetime
and not withdate
Then it is necessary 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
Using strftime()
.
>>> import datetime
>>> hoje = datetime.date(2015, 9, 30)
>>> int(hoje.strftime('%s')) * 1000
1441681200000
See that it is
%s
with s lower case, not upper case. Use s uppercase will return only the seconds of the date (in this case00
) and not the total seconds since 1970.
Remarks:
I found no reference to s lowercase in documentation, but works in Python 3 and 2 on Mac OS.
The
strftime('%s')
returns only the rounded total seconds, preventing accuracy in milliseconds.
Using mktime()
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 accuracy for milliseconds.
Using total_seconds()
of timedelta
:
By making the difference between two datetime
is returned a timedelta
and from it it is possible to 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 adatetime.datetime(1970, 1, 1, 0, 0)
as mentioned by mgibsonbr.
Note that some of these solutions have limited accuracy in seconds, and others may not be portable (e.g., windows may not support %s
). P.S. In the first two solutions, it would not be interesting to multiply by 1000 before to convert to int
, and not after? (In the 3rd it doesn’t matter, I don’t know about the 2nd, but the 1st has enough precision for such)
@mgibsonbr really the right is to convert to int
after multiplication, I was testing datetime
without putting in hours and minutes so always returned .0
in the end I ended up getting confused. Regarding not working on windows I no longer know, I am using Mac OS
Mac OS is POSIX, so the %s
works even. It is on Windows that it may or may not be implemented (and in my case - Windows 7, Python 2.7 and 3.4 - it was not implemented).
1
Since the datetime object already has a method to represent a date in seconds since January 1, 1970, the following suffices:
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())
Remembering that timestamp()
only works in Python 3
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
milestones since?
– ppalacios
Since 1 January 1970.
– Paulo