How to make a Digital Clock from Python
- Vaidik Pandya
- May 4, 2021
- 2 min read
Scientists can now produce a clock that ticks at an amazingly accurate rate of one second every billion years. In this post, we will build our own clock in python to demonstrate how it works.

The first thing you need is import the time and DateTime modules. We also need to import math so that we can use it to compute the hours and minutes for each day.
We start by initializing an empty DateTime object with today's date-time information and store the result in now.
Now, we will loop through each day on the weekdays and compute how many minutes and hours each date-time object has. We do this by doing a subtraction of minutes and hours from the total number of minutes in a day, which is 1440.
We then print the hours and minutes for each date-time object to standard output.
The new values for hours and minutes are then stored into now so that it can process it next time it runs through the loop.
We also want to add some seconds in order to obtain an accurate representation of continuous seconds. We do this by adding the total number of seconds in an hour and a minute to the hours and minutes values.
Then, we print the seconds for each date-time object to standard output.
We can see that minutes for each date-time object increase by 1 every second. On the other hand, hours are still calculated using a total number of minutes in a day which is 1440. This makes it so that hours increase by 0 every second which is why we want to store it now so that it can process it again next time it runs through the loop.
The new values for times are then stored into now so that it can process the next time it runs through the loop.
CODE = https://drive.google.com/file/d/110MfBnDZombwO4Iw5YmZZxQzmo4-DYIn/view?usp=sharing
Comments