0

So I need to display current time, I only print now.hour and now.minutes, however there is always 00 that's displayed for seconds that I would like to take away

this is my code

    now = datetime.datetime.now()
    print(datetime.time(now.hour, now.minute))

this is my out put

16:40:00
  • Simply don't construct a new time object to print. Try `print(f"{now.hour}:{now.minute}")`. Note that this _will not_ add leading zeros or other useful time formatting, but that can be added trivially by referencing the documentation for formatting strings. – Brian61354270 Feb 16 '20 at 22:50
  • Have you tried anything, done any research? – AMC Feb 16 '20 at 23:27
  • https://stackoverflow.com/questions/2158347/how-do-i-turn-a-python-datetime-into-a-string-with-readable-format-date – AMC Feb 16 '20 at 23:28

1 Answers1

1

You can use strftime to format the time stored in now.

now
>2020-02-17 09:47:52.429173

print(datetime.strftime(now, '%H:%M'))
09:47

The string located in datetime.strftime(--, 'here') defines the formatting that will be returned. You can find more of strftime's formatting specifications here.

PacketLoss
  • 5,561
  • 1
  • 9
  • 27