Python 3 Script to Build 24 & 12 Hour Time Format Digital Clock in Terminal Using time Module
545 views
Jun 3, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
So in this video I will show you how to
0:04
create a simple digital clock inside the
0:08
terminal inside Python in various
0:10
formats 24hour format or 12 hour format
0:15
How to display the current date and time
0:17
So I just written this simple Python
0:19
script Let me run this Python script So
0:21
it will output the current date and time
0:25
If I execute this you will see in
0:27
various formats So it will output the
0:31
current time in 24-hour format which is
0:34
this one and 12hour format this time and
0:39
the current date So all these things it
0:42
will print out something like a digital
0:44
digital clock in the terminal You can
0:46
easily do this using the built-in module
0:49
time and operating system Uh I will show
0:53
you step by step the script in this
0:57
video So first of all you just need to
0:59
require the time module and the
1:01
operating system module Just require
1:04
both these modules After that we will
1:07
actually
1:09
uh call our main function
1:18
So we just call a main function So we
1:21
just need to now define the main
1:23
function right here
1:27
So inside this we
1:29
will run a simple while loop while true
1:34
This will run indefinite number of time
1:38
And here we'll be creating three
1:40
variables The current time in 24hour
1:45
format And for this we'll be using the
1:47
time module And we'll be using this
1:49
method
1:51
str time and it will print out the time
1:55
in three different formats in uh 24-hour
1:59
format and the 12-hour format So you can
2:03
see we are using this function and here
2:07
we are specifying this
2:09
format to get the time in 24hour and
2:13
12-hour format and then we can simply
2:15
print out the current date by using this
2:19
again this function
2:24
strf to print out the current
2:31
eight So after doing
2:34
this we will print out everything using
2:37
these three print statements and then we
2:40
will sleep
2:43
for 1
2:46
second So this is really important We
2:49
just need to sleep for 1 second in
2:52
between pause for updates
2:56
So so again we just need to clear out
2:59
the screen as well For clearing out the
3:01
screen here we are calling this function
3:03
clear screen So we just need to define
3:06
this function which is
3:08
clear
3:11
screen So this will use the operating
3:14
system module to clear the previous
3:17
content and uh using this space clear
3:21
out the previous content and then so
3:23
once again if you run this now
3:27
just it is saying that current date is
3:30
not defined
3:33
uh
3:34
current date Oh sorry I think we
3:39
forgot just create this variable where
3:42
we will be storing the current
3:47
date So you can see now we basically
3:51
built out this digital clock which runs
3:53
entirely in the terminal It shows your
3:56
current time in 24hour format 12-hour
3:59
format and also it print outs the
4:01
current date as well So in this way you
4:04
can build out a very simple digital
4:06
clock inside your terminal which will
4:09
show the current time So thank you very
4:12
much for watching this video and also
4:14
check out my website freemediatools.com
4:17
uh which contains thousands of tools