How do I convert a human-readable time such as 20.12.2016 09:38:42,76 to a Unix timestamp in milliseconds?
 
    
    - 24,552
- 19
- 101
- 135
 
    
    - 947
- 1
- 7
- 9
- 
                    1You need to provide an appropriate format string to `datetime.datetime.strptime`. See [the docs](https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior). – a_guest Jan 13 '17 at 13:27
7 Answers
In Python 3 this can be done in 2 steps:
- Convert timestring to datetimeobject
- Multiply the timestamp of the datetimeobject by 1000 to convert it to milliseconds.
For example like this:
from datetime import datetime
dt_obj = datetime.strptime('20.12.2016 09:38:42,76',
                           '%d.%m.%Y %H:%M:%S,%f')
millisec = dt_obj.timestamp() * 1000
print(millisec)
Output:
1482223122760.0
strptime accepts your timestring and a format string as input. The timestring (first argument) specifies what you actually want to convert to a datetime object. The format string (second argument) specifies the actual format of the string that you have passed.
Here is the explanation of the format specifiers from the official documentation:
- %d- Day of the month as a zero-padded decimal number.
- %m- Month as a zero-padded decimal number.
- %Y- Year with century as a decimal number
- %H- Hour (24-hour clock) as a zero-padded decimal number.
- %M- Minute as a zero-padded decimal number.
- %S- Second as a zero-padded decimal number.
- %f- Microsecond as a decimal number, zero-padded to 6 digits.
 
    
    - 3,172
- 3
- 19
- 21
- 
                    1Hey, thanks. Unfortunately I got the error "AttributeError: 'datetime.datetime' object has no attribute 'timestamp'". Possibly because I am using Python 2.7? – Tom Jan 13 '17 at 14:05
- 
                    1
For those who search for an answer without parsing and losing milliseconds,
given dt_obj is a datetime:
python3 only, elegant
int(dt_obj.timestamp() * 1000)
both python2 and python3 compatible:
import time
int(time.mktime(dt_obj.utctimetuple()) * 1000 + dt_obj.microsecond / 1000)
 
    
    - 5,992
- 12
- 61
- 78
 
    
    - 6,407
- 3
- 30
- 28
For Python2.7 - modifying MYGz's answer to not strip milliseconds:
from datetime import datetime
d = datetime.strptime("20.12.2016 09:38:42,76", "%d.%m.%Y %H:%M:%S,%f").strftime('%s.%f')
d_in_ms = int(float(d)*1000)
print(d_in_ms)
print(datetime.fromtimestamp(float(d)))
Output:
1482248322760
2016-12-20 09:38:42.760000
 
    
    - 1,047
- 10
- 23
You need to parse your time format using strptime.
>>> import time
>>> from datetime import datetime
>>> ts, ms = '20.12.2016 09:38:42,76'.split(',')
>>> ts
'20.12.2016 09:38:42'
>>> ms
'76'
>>> dt = datetime.strptime(ts, '%d.%m.%Y %H:%M:%S')
>>> time.mktime(dt.timetuple())*1000 + int(ms)*10
1482223122760.0
 
    
    - 17,279
- 4
- 30
- 47
For Python2.7
You can format it into seconds and then multiply by 1000 to convert to millisecond.
from datetime import datetime
d = datetime.strptime("20.12.2016 09:38:42,76", "%d.%m.%Y %H:%M:%S,%f").strftime('%s')
d_in_ms = int(d)*1000
print(d_in_ms)
print(datetime.fromtimestamp(float(d)))
Output:
1482206922000
2016-12-20 09:38:42
 
    
    - 16,554
- 10
- 50
- 78
- 
                    2Hey, thanks! I copy and paste your code and got a "ValueError: Invalid format string". – Tom Jan 13 '17 at 14:15
- 
                    
- 
                    
- 
                    
- 
                    1
- 
                    It will ignore millisecond in string. And won't solve the problem. – PageNotFound Dec 19 '17 at 03:54
- 
                    I am using Python 2.7.16, saying `ValueError: Invalid format string`. I guess it's b/c `%s` is not supported. Only `%S` is supported. – Jing He Jun 12 '19 at 07:50
A lot of these answers don't preserve the milliseconds from the datetime. This works for me
def datetime_to_ms_epoch(dt):
    microseconds = time.mktime(dt.timetuple()) * 1000000 + dt.microsecond
    return int(round(microseconds / float(1000)))
 
    
    - 9,376
- 15
- 62
- 109
Simple python 2.7 / 3 solution for converting python datetime to timestamp (as int) as title suggests. Use datetime.strptime to convert string to datetime object if your input is a string.
from datetime import datetime
dt_obj = datetime.utcnow()  # input datetime object
milliseconds
int(float(dt_obj.strftime('%s.%f')) * 1e3)
1656096296215
microseconds
int(float(dt_obj.strftime('%s.%f')) * 1e6)
1656096296215242
 
    
    - 8,487
- 6
- 54
- 53