0

As part of my new work I need to convert one existing java class to a python one.

person.setDob(String.valueOf(person.getDateOfBirth().getTime()));

Please see the above snippet here how to fetch time in milliseconds from date object in python, Hope I can use datetime.datetime for this purpose. Please help.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Anoop M Nair
  • 1,057
  • 1
  • 13
  • 31

2 Answers2

1

To get a date string with milliseconds (3 decimal places behind seconds), use this:

from datetime import datetime

print datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]

OUTPUT 2018-10-04 10:18:32.926

%f is displaying milliseconds

Cipher
  • 2,060
  • 3
  • 30
  • 58
0

To get milliseconds in python, use the below code.

 import datetime
 print('Datetime in milliscond using now()',datetime.datetime.now())
 print('Datetime in milliscond using utcfromtimestamp()', datetime.datetime.utcfromtimestamp(0))

output looks like below

 Datetime in milliscond using now() 2019-03-11 17:34:28.290409
 Datetime in milliscond using now() 1970-01-01 00:00:00
Anjali
  • 1,680
  • 4
  • 26
  • 48