Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (19k points)

I'm adding UTC time strings to Bitbucket API responses that currently only contain Amsterdam (!) time strings. For consistency with the UTC time strings returned elsewhere, the desired format is 2011-11-03 11:07:04 (followed by +00:00, but that's not germane).

What's the best way to create such a string (without a microsecond component) from a datetime instance with a microsecond component?

>>> import datetime

>>> print unicode( 

2011-11-03 11:13:39.278026

1 Answer

0 votes
by (106k points)

The best way to create a string without a microsecond component from a datetime instance is by using a datetime object in a specific format that is different from the standard format. You can do it like mentioned below:-

import datetime"%Y-%m-%d %H:%M:%S") 


Another thing you can use the now() function from the datetime module. 

import datetime 

now =



You can refer to our Python online course for more information.

Browse Categories