If you have a datetime
object, could could just keep it around in your code as a datetime
object, extracting whatever information you need from it. Then when you really need the actual string, use strftime
to format it in the way you want.
>>> from datetime import datetime
>>> t = datetime.now()
>>> t
datetime.datetime(2019, 6, 24, 14, 23, 45, 835379)
>>> print(t.month)
6
>>> print(t.second)
45
>>> as_string = t.strftime("%B %d, %Y")
>>> print(as_string)
June 24, 2019
>>> as_another_string = t.strftime("%Y-%h-%d %H:%m")
>>> print(as_another_string)
2019-Jun-24 14:06
This page shows you the sorts of format codes you can call upon, in order to extract whichever date/time information you want to display in your string: