python date and time converted to a string or an instance of formatted output

  • 2020-10-23 21:03:37
  • OfStack

As shown below:

Years, months, days, minutes and seconds


>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
2017-07-15 15:01:35

Years, months, days, hours and minutes


>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
2017-07-15 15:01

(date) (month) (year)


>>> print datetime.datetime.now().strftime("%Y%m%d")
20170715

Related articles: