I need a datetime to retain the microseconds even though they are 0. Here is an example:
from datetime import datetime
starttime = datetime(year=2018, month=2, day=15, hour=8, minute=0, second=0, microsecond=0)
print(starttime.isoformat())
I want that to print with 00.000000
seconds. But it prints with 0
seconds. If I put in 1 microsecond then it will print 00.000001
.
The issue is that I am using a jquery call that requires the iso format with a float in the seconds position.
You can manually format it with strftime
:
>>> starttime = datetime(year=2018, month=2, day=15, hour=8, minute=0, second=0, microsecond=0)
>>> starttime.strftime("%Y-%m-%dT%H:%M:%S.%f")
'2018-02-15T08:00:00.000000'
Vs:
>>> starttime.isoformat()
'2018-02-15T08:00:00'
And:
>>> starttime = datetime(year=2018, month=2, day=15, hour=8, minute=0, second=0, microsecond=1)
>>> starttime.strftime("%Y-%m-%dT%H:%M:%S.%f")
'2018-02-15T08:00:00.000001'
>>> starttime.isoformat()
'2018-02-15T08:00:00.000001'
(Confirmed to work after Python3.6, will NOT work on 2.7)
isoformat()
accepts an argument:
datetime.datetime(2020, 2, 20, 0, 0, 0, 0).isoformat(timespec='microseconds')
Out[17]: '2020-02-20T00:00:00.000000'
possible values:
'auto': the default behaviour
'hours': '{:02d}'
'minutes': '{:02d}:{:02d}'
'seconds': '{:02d}:{:02d}:{:02d}'
'milliseconds': '{:02d}:{:02d}:{:02d}.{:03d}'
'microseconds': '{:02d}:{:02d}:{:02d}.{:06d}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With