I have the time like 2021-05-03 14:51:56.769715
. I need to convert into readable string like
May 3, 2021, 2:51:56 PM
in python.I need to pass the timezone also while converting it.
Is there any way we can do it in python?
Thanks in Advance :)
I have the time like 2021-05-03 14:51:56.769715
. I need to convert into readable string like
May 3, 2021, 2:51:56 PM
in python.I need to pass the timezone also while converting it.
Is there any way we can do it in python?
Thanks in Advance :)
This should be OK with the format you want:
from datetime import datetime
# Original string
str_date = '2021-05-03 14:51:56.769715'
# Datetime object creation from original string
obj_date = datetime.strptime(str_date, '%Y-%m-%d %H:%M:%S.%f')
# Result string with the desired format
converted_str = datetime.strftime(obj_date, '%b %-d, %Y, %-I:%-M:%-S %p')
# Print the result
print(converted_str)
The above code would print May 3, 2021, 2:51:56 PM
, just as you expect. However, I have guessed that you would prefer not to have zero-padded minutes and seconds, but if you would like to you should then use %M
and %S
instead of %-M
and %-S
.
For more information about date formatting in Python you should check the Python strftime reference