Some people think that getting the time in python is hard and time consuming. However, truth be told, getting the time is simple and a straightforward process. I created a python-based program that will get the current time and print the time in the format “the time is 4:54pm”
What Does this Program do?
This program will get the current time and print the time in the format “the time is 4:54pm”.
Steps to Getting the Time
Some people think that getting the time in python is hard and time consuming. However, truth be told, getting the time is simple and a straight forward process.
On a high level, below are the list of steps to record someone:
i. Getting the date, time, and second
ii. Take the data and second out
iii. Put it in the format you wan’t
iv. Print the time
Step #1: Install Packages
These instructions are meant for python 3.8, if you have an older version you can upgrade or try to follow along, but the code won’t be exactly the same.
Before you start writing python codes, let’s install some packages.
Go to the terminal and type “pip install” with the package name. An example is, “pip install datetime” and press “enter” key.
Similarly, install all the packages listed in the table below, in different lines in the terminal:
If any of these packages have already been installed on your computer, you will get the below message “Requirement already satisfied”:
If the package installation works, it will look like something like the below image “Successfully installed”:
If there is an error, there will be a red message like the below image “ERROR: No matching distribution”. Fix the error using the correct package name and successfully install all these packages.
Once all the above packages are successfully installed, it’s time to move on to importing the packages in the program and start coding.
Step #2: Import Packages
In this step you will import datetime so you can get the date and time in your program.
from datetime import datetime
Step #3: Create the Variables
In this step you create a variable called end and store “AM” in it. You will also create a variable called final and store “The Time is ” in it.
end = "AM"
final = "The time is "
Step #4: Get the Date, Time and Second
In this step you will get the date time and second using datetime, then you will store that value in a variable called now.
now = datetime.now()
Step #5: Change Format to Hour, Minute
In this step you will take the variable now and change the format so it is Hour : Minute. E.g 10:37. Then we save that value in a variable called current_time.
current_time = now.strftime("%H:%M")
Step #6: Change Format to List
In this we change the variable current_time to a list.
current_time = list(current_time)
Step #7: Checking Army Time
In this step, you will add an if statement to check if the list is longer than 4 characters and an if statement to check if the hour is after 12. We check this because the time comes in army time which is 0 to 24 hours, no AM or PM. If the length of the list is longer than 4 characters and the hour is after 12 then it will be 13:00, 14:00 and so on, but we want 1:00pm, 2:00pm, and so on. An example is if the time is 16:00 army time, we want it to be 4:00pm, so we need to change it.
if len(current_time) > 4 and int(current_time[0] + current_time[1]) > 12:
Step #8: Changing to Army Time
In this step, you will change the time to standard time format, if needed. You will also change the variable called end to “PM”.
current_time[1] = int(str(current_time[0]) + str(current_time[1])) - 12
current_time[0] = ""
end = 'PM'
Step #9: Adding to Final Time
In this step, you will create a for loop that goes through the list called current_time and add the character that it is on to the variable final.
for i in current_time:
final += str(i)
Step #10: Adding the AM/PM
In this step, you will add the variable end to the variable final and then you will print final.
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)
Thanks for staying till the end. If you enjoyed this article, please follow me for updates on new articles. Medium is where I want to give back to the community and help others learn coding. I welcome responses related to this article and its views. Please leave a response on what else you would like to learn and I’ll try to teach that at some point.
Comments