You can generate time stamp in your aforementioned format: YYYY-MM-DD hh:mm:ss:uuuuuu
where u
represents microseconds.
For dt
being a datetime timestamp, you can use isoformat()
function. Here, dt.isoformat(timespec='microseconds')
will return your desired result (except that the final colon is turned into a decimal point, and that there is a 'T' between date and time).
If you really want that colon, use formatted output and fields from that datetime object:
"%4d-%02d-%02d %02d:%02d:%02d:%04d"
%(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond // 100)
Here are some sample code with inputs and outputs:
dt = datetime(2018, 5, 21, 0, 0, 0, 100)
print(dt.isoformat(timespec='microseconds'))
print("%4d-%02d-%02d %02d:%02d:%02d:%04d"
%(dt.year, dt.month, dt.day,
dt.hour, dt.minute, dt.second, dt.microsecond // 100))
This outputs
2018-05-21T00:00:00.000100
2018-05-21 00:00:00:0001