Getting the Current Time in Python

What Does this Program do?

Steps to Getting the Time

Step #1: Install Packages

Step #2: Import Packages

from datetime import datetime

Step #3: Create the Variables

end = "AM"
final = "The time is "

Step #4: Get the Date, Time and Second

now = datetime.now()

Step #5: Change Format to Hour, Minute

current_time = now.strftime("%H:%M")

Step #6: Change Format to List

current_time = list(current_time)

Step #7: Checking Army Time

if len(current_time) > 4 and int(current_time[0] + current_time[1]) > 12:

Step #8: Changing to Army Time

current_time[1] = int(str(current_time[0]) + str(current_time[1])) - 12
current_time[0] = ""

end = 'PM'

Step #9: Adding to Final Time

for i in current_time:
final += str(i)

Step #10: Adding the AM/PM

final += end
print(final)

Below is the Complete Source Code

from datetime import datetime

end = "AM"
final = "The time is "

now = datetime.now()
current_time = now.strftime("%H:%M")
current_time = list(current_time)

if len(current_time) > 4 and int(current_time[0] + current_time[1]) > 12:
current_time[1] = int(str(current_time[0]) + str(current_time[1])) - 12
current_time[0] = ""

end = 'PM'

for i in current_time:
final += str(i)

final += end
print(final)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store