0

I want to display time only by "Hours:Minutes".

For example, if it's 9:30 AM, it'll only display 9:30.

If it's 10:20 PM, it'll only display 10:20.

Doesn't matter if "AM" or "PM" won't show.

I googled "how to display time to 12 hours python" and I see this same method below here and other similar threads:

from datetime import datetime
d = datetime.strptime("10:30", "%H:%M")
d.strftime("%I:%M %p")
print(d.strftime("%I:%M %p"))

The problem is... it always shows "10:30 AM".

What if my current time is 9:20 PM? How will that be changed?

What if I want to show my current time hundred times?

I can't change the first argument in "d = datetime.strptime("10:30", "%H:%M")" hundred times. Not a good idea.

yalpsid
  • 235
  • 1
  • 13

1 Answers1

1

This doesn't make sense. You're taking the hard-coded string "10:30", converting it to a datetime, and then converting it back to a string. So of course it always says "10.30".

If you want the current time, then get it directly:

d = datetime.datetime.now()
print(d.strftime("%I:%M"))
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895