Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

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 101 views
1 answer 178 views
2 answers 187 views
2 answers 155 views
3 answers 221 views
...