The eopch time wont include milliseconds so currently would only be 10 digits long. Yours is 13, you can get rid of the extra milliseconds by floor dividing by 1000
from datetime import datetime
msgTime = 1540406475702
print (msgTime)
messageTime=datetime.utcfromtimestamp(msgTime//1000)
print(messageTime)
OUTPUT
1540406475702
2018-10-24 18:41:15
UPDATE
Having looked at the source code of datetime and how it implements fromtimestamp if you wanted to pass milliseconds you can do it by passing the values as a float where the 3 digits after the decimal point represents the milliseconds. Below is an extract from the datetime module
@classmethod
def _fromtimestamp(cls, t, utc, tz):
"""Construct a datetime from a POSIX timestamp (like time.time()).
A timezone info object may be passed in as well.
"""
frac, t = _math.modf(t)
us = round(frac * 1e6)
if us >= 1000000:
t += 1
us -= 1000000
elif us < 0:
t -= 1
us += 1000000
converter = _time.gmtime if utc else _time.localtime
y, m, d, hh, mm, ss, weekday, jday, dst = converter(t)
ss = min(ss, 59) # clamp out leap seconds if the platform has them
result = cls(y, m, d, hh, mm, ss, us, tz)
So you can pass milli / micro seconds if you pass them as the digits after the decimal place. Is it possible when this was working before that your epoch was second followed by a decimal place then the milliseconds?
from datetime import datetime
msgTime = 1540406475.702
print (msgTime)
messageTime=datetime.utcfromtimestamp(msgTime)
print(messageTime)
OUTPUT
1540406475.702
2018-10-24 18:41:15.702000