Python datetime to string without microsecond component Python datetime to string without microsecond component python python

Python datetime to string without microsecond component


If you want to format a datetime object in a specific format that is different from the standard format, it's best to explicitly specify that format:

>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")'2011-11-03 18:21:26'

See the documentation of datetime.strftime() for an explanation of the % directives.


>>> import datetime>>> now = datetime.datetime.now()>>> print unicode(now.replace(microsecond=0))2011-11-03 11:19:07


In Python 3.6:

from datetime import datetimedatetime.now().isoformat(' ', 'seconds')'2017-01-11 14:41:33'

https://docs.python.org/3.6/library/datetime.html#datetime.datetime.isoformat


matomo