Ad Code

✨🎆 Codex 1.0 PLACEMENT READY PROGRAM! 🎆✨

Get 75% Discount Early bird offer CLICK to JOIN CodeX 1.0 click

Time module in python

Python Time Module

Python time Module – Complete Guide With Examples

The time module in Python is used to work with date & time values, timestamps, delays, stopwatch features, and time formatting. It is one of the most important built-in modules when you want to:

  • Display the current date and time
  • Measure execution time of programs
  • Convert timestamps into readable formats
  • Pause program execution (sleep)
  • Calculate time differences

Basic Example of the Time Module

import time

print(time.time())                      # returns current timestamp (seconds since 1970)
print(time.localtime(time.time()))      # converts timestamp to local time structure
print(time.asctime(time.localtime(time.time())))  # converts to readable date-time format

Output Example:

1735942230.7655
time.struct_time(tm_year=2025, tm_mon=1, tm_mday=15, tm_hour=11, tm_min=10, tm_sec=30, ...)
Wed Jan 15 11:10:30 2025

Breakdown of time.struct_time Values

When you call time.localtime(), Python returns a structure containing:

  • tm_year – Year
  • tm_mon – Month
  • tm_mday – Day of month
  • tm_hour – Hours
  • tm_min – Minutes
  • tm_sec – Seconds
  • tm_wday – Day of week
  • tm_yday – Day of year

Example: Display Custom Date Format

import time

d = time.localtime(time.time())
print(str(d.tm_year) + ":" + str(d.tm_mon) + ":" + str(d.tm_mday))

Useful Functions of the Time Module

1. time.sleep() – Pause Program Execution

import time

print("Start")
time.sleep(2)     # waits 2 seconds
print("End")

2. Measure Execution Time (Stopwatch)

import time

start = time.time()

# some code
for i in range(1000000):
    pass

end = time.time()

print("Execution time:", end - start, "seconds")

3. Get Current Time in Different Formats

import time

print(time.strftime("%Y-%m-%d"))
print(time.strftime("%H:%M:%S"))
print(time.strftime("%d-%m-%Y %I:%M:%S %p"))

Output Example:

2025-01-15
11:18:25
15-01-2025 11:18:25 AM

Formatting Codes for strftime()

Format CodeDescription
%Y4-digit year
%y2-digit year
%mMonth (01–12)
%dDay (01–31)
%HHour (24-hour)
%IHour (12-hour)
%MMinutes
%SSeconds
%pAM/PM
%AFull weekday name
%BFull month name

Example: Display Full Date and Time

import time

print(time.strftime("%A, %d %B %Y - %I:%M:%S %p"))
Output:
Wednesday, 15 January 2025 - 11:25:39 AM

Example: Countdown Timer

import time

for i in range(5, 0, -1):
    print(i)
    time.sleep(1)

print("Time Up!")

Assignment – Time Module Programs

  1. WAP to create a digital clock that updates every second.
  2. WAP to measure execution time of bubble sort using the time module.
  3. WAP to print today’s date in format: DD/MM/YYYY.
  4. WAP to create a stopwatch with Start/Stop features.
  5. WAP to calculate difference between two timestamps.

Post a Comment

3 Comments

  1. import time
    print(time.time)
    print(time.localtime(time.time()))
    print(time.asctime(time.localtime(time.time())))

    ReplyDelete
  2. import time
    second = time.time()
    print("second since epoch", second)

    ReplyDelete
  3. import time
    second = time.time()
    print("second since epoch", second)

    ReplyDelete

POST Answer of Questions and ASK to Doubt