Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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(datetime.datetime.now()) 

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

datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 

image

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

import datetime 

now = datetime.datetime.now()

print(unicode(now.replace(microsecond=0)))

image

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

Browse Categories

...