Is there any simple way to create a pattern in datetime? If I have a timestamp that looks like this
2019-04-28 21:22:00
I could create a pattern in Java that looks like this
yyyy-MM-dd HH:mm:ss
Is there something similar in Python?
Is there any simple way to create a pattern in datetime? If I have a timestamp that looks like this
2019-04-28 21:22:00
I could create a pattern in Java that looks like this
yyyy-MM-dd HH:mm:ss
Is there something similar in Python?
You can do this using the strftime method of the datetime
object from the datetime library.
In your case you would use the following format string:
"%Y-%m-%d %H:%M:%S"
Example usage:
import datetime as dt
my_time = dt.datetime.now()
my_formatted_time = my_time.strftime("%Y-%m-%d %H:%M:%S")
print(my_formatted_time)
with result:
2019-04-28 12:30:46