Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get single digit time in python

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?

like image 680
coldcake71 Avatar asked Oct 29 '25 04:10

coldcake71


1 Answers

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/

like image 193
Nikolas Stevenson-Molnar Avatar answered Oct 31 '25 06:10

Nikolas Stevenson-Molnar