How to Create a Datetime in Python from Milliseconds

How do I create a datetime in Python from milliseconds?

Just convert it to timestamp

datetime.datetime.fromtimestamp(ms/1000.0)

simple way to drop milliseconds from python datetime.datetime object

You can use datetime.replace() method -

>>> d = datetime.datetime.today().replace(microsecond=0)
>>> d
datetime.datetime(2015, 7, 18, 9, 50, 20)

How to add milliseconds to a datetime object

Use strftime() to print your datetime in whatever format you want.

The built-in Datetime only really supports six-digit microseconds using strftime(), with the format code %f:

dt = datetime(2020,2, 3, 10, 0, 0)
dt.strftime('%Y-%m-%d %H:%M:%S:%f')
# '2020-02-03 10:00:00:000000'

You can use a string slice to cut off the latter three digits if you want (leaving only the milliseconds):

dt.strftime('%Y-%m-%d %H:%M:%S:%f')[:-3]
'2020-02-03 10:00:00:000'

Convert python datetime to timestamp in milliseconds

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 to 6 digits.

How to convert millisecond time stamp to normal date in Python?

you can do this by using to_datetime function https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html.

df['Millisecond'] = pd.to_datetime(df['Millisecond'], unit='ms')

Converting epoch time with milliseconds to datetime

Use datetime.datetime.fromtimestamp:

>>> import datetime
>>> s = 1236472051807 / 1000.0
>>> datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S.%f')
'2009-03-08 09:27:31.807000'

%f directive is only supported by datetime.datetime.strftime, not by time.strftime.

UPDATE Alternative using %, str.format:

>>> import time
>>> s, ms = divmod(1236472051807, 1000) # (1236472051, 807)
>>> '%s.%03d' % (time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(s)), ms)
'2009-03-08 00:27:31.807'
>>> '{}.{:03d}'.format(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(s)), ms)
'2009-03-08 00:27:31.807'

Convert milliseconds to string %H%M%S.%f

create your timedelta object

from datetime import timedelta

ms = 23500
time = timedelta(milliseconds=ms)

now print the result

print(time) # output: 0:00:02.35

or save the result in a variable

result = str(time)


Related Topics



Leave a reply



Submit