I am using following code to get current time in python.
import datetime
now = datetime.datetime.now()
message_sent_time = now.strftime("%I:%M %p")
It returns time as
06:58 PM
However, I would like to get output as
6:58 PM
i.e. single digit time when time is before 10. How can I achieve it?
You can get a single-digit hour using %-I:
>>> message_sent_time = now.strftime("%-I:%M %p")
>>> print(message_sent_time)
7:07 PM
Full strftime formatting reference: http://strftime.org/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With