Convert python datetime to timestamp in milliseconds

前端 未结 5 664
你的背包
你的背包 2020-12-05 04:18

How to convert a human readable time with the format 20.12.2016 09:38:42,76 to Unix timestamps in milliseconds? I found a lot of similar questi

相关标签:
5条回答
  • 2020-12-05 04:41

    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
    
    0 讨论(0)
  • 2020-12-05 04:43

    In Python 3 this can be done in 2 steps:

    1. Convert timestring to datetime object
    2. Multiply the timestamp of the datetime object 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 on the left.
    0 讨论(0)
  • 2020-12-05 04:43

    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
    
    0 讨论(0)
  • 2020-12-05 04:44

    For those who searches for an answer without parsing and loosing 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)
    
    0 讨论(0)
  • 2020-12-05 04:45

    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
    
    0 讨论(0)
提交回复
热议问题