How to format a datetime into a string with milliseconds in Python

2 Answers

0 votes
from datetime import datetime 
   
print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-1])
print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-2])
 
s = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
print(s)
 
 
 
'''
run:
 
2020-01-03 17:09:01.57884
2020-01-03 17:09:01.5789
2020-01-03 17:09:01.578
 
'''

 



answered Jan 3, 2020 by avibootz
edited Jan 4, 2020 by avibootz
0 votes
from datetime import datetime 
   
print(datetime.utcnow().isoformat(sep=' ', timespec='milliseconds'))
print(datetime.now().isoformat(sep=' ', timespec='milliseconds'))
 
 
 
'''
run:
 
2020-01-04 20:16:06.330
2020-01-04 20:16:06.330
 
'''

 



answered Jan 4, 2020 by avibootz

Related questions

2 answers 118 views
1 answer 205 views
2 answers 206 views
2 answers 181 views
3 answers 240 views
...