Home > OS >  python datetime with 6 digit milliseconds
python datetime with 6 digit milliseconds

Time:01-11

How could I print 6 digit milli seconds in below format

>>> import datetime
>>> datetime.datetime.now(tz = datetime.datetime.now().astimezone().tzinfo).isoformat(timespec='milliseconds')
'2022-01-10T18:29:10.698000 05:30'

Actual Output:

 '2022-01-10T18:29:10.108 05:30'

Expecting Output something like:

  '2022-01-10T18:29:10.108000 05:30'

CodePudding user response:

Use timespec=microseconds:

>>> dt = datetime.datetime.now(tz = datetime.datetime.now().astimezone().tzinfo).isoformat(timespec='microseconds')
'2022-01-10T14:05:55.742931 01:00'

Update:

If you want 0 for microsecond value, you can do:

now = datetime.datetime.now().astimezone()
now = now.replace(microsecond=now.microsecond // 1000 * 1000)
now = now.isoformat(timespec='microseconds')
print(now)

# Output
'2022-01-10T14:17:08.386000 01:00'

CodePudding user response:

There are only 1000ms in 1 second, do you mean microseconds?

datetime.datetime.now(tz = datetime.datetime.now().astimezone().tzinfo).isoformat(timespec='microseconds')

CodePudding user response:

If you want utc and milliseconds value, you can do

UTC Convertion

import pytz
from datetime import datetime
epoch: datetime = datetime.now().replace(tzinfo=pytz.utc)
print(epoch)
  •  Tags:  
  • Related