- Home
- /
- Python Date & Time
Python Tutorial
Introduction
Python Data Types & Oper...
Python Strings
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Conditional Statements
Python Loops
Python Functions
Python Modules
Python OOPS
Advanced Topics
File Handling
- Home
- /
- Python Date & Time
Python Date & Time
Time plays a big role in programming—whether you’re tracking when a file was updated or delaying code execution. Python provides powerful tools for working with date and time, mainly through the time
and calendar
modules.
time.time(): Ticks Since Epoch
Python tracks time in seconds since January 1, 1970 (Epoch time).
import time
print(time.time())
Output:
1720154981.6328802
This number is known as a tick—a floating point value counting seconds since the epoch.
Human-Readable Time with time.ctime()
To see the current time in a readable format:
import time
print(time.ctime())
Output:
Fri Jul 05 14:29:41 2025
time.sleep(): Pause the Code
You can pause the program for a few seconds using sleep()
:
import time
print("Wait for 3 seconds...")
time.sleep(3)
print("Done waiting!")
Output:
Wait for 3 seconds...
Done waiting!
Working with struct_time
Python represents time as a struct_time
object with 9 fields:
Index | Field | Example | Description |
---|---|---|---|
0 | tm_year | 2025 | Year |
1 | tm_mon | 7 | Month (1–12) |
2 | tm_mday | 5 | Day of the month |
3 | tm_hour | 14 | Hour (0–23) |
4 | tm_min | 29 | Minute (0–59) |
5 | tm_sec | 41 | Second (0–61) |
6 | tm_wday | 5 | Day of week (Mon=0) |
7 | tm_yday | 186 | Day of year (1–366) |
8 | tm_isdst | 0 | Daylight Saving Time |
Local Time – time.localtime()
Returns the local time in struct_time
form.
import time
print(time.localtime(1720154981.6328802))
Output:
time.struct_time(tm_year=2025, tm_mon=7, tm_mday=5, tm_hour=14, tm_min=29, tm_sec=41, tm_wday=5, tm_yday=186, tm_isdst=0)
UTC Time – time.gmtime()
Returns UTC time (not your local time):
import time
print(time.gmtime(1720154981.6328802))
Output:
time.struct_time(tm_year=2025, tm_mon=7, tm_mday=5, tm_hour=9, tm_min=29, tm_sec=41, tm_wday=5, tm_yday=186, tm_isdst=0)
time.mktime(): Convert to Epoch Time
Converts a local struct_time
to ticks (seconds since epoch):
import time
my_time = (2025, 7, 5, 14, 0, 0, 5, 186, 0)
print(time.mktime(my_time))
Output:
1720154400.0
time.asctime(): struct_time → String
Converts a struct_time
to a string format:
import time
my_time = (2025, 7, 5, 14, 0, 0, 5, 186, 0)
print(time.asctime(my_time))
Output:
Sat Jul 5 14:00:00 2025
time.strptime(): String → struct_time
Parses a date string into a struct_time
:
import time
date_str = "5 July, 2025"
print(time.strptime(date_str, "%d %B, %Y"))
Output:
time.struct_time(tm_year=2025, tm_mon=7, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=186, tm_isdst=-1)
Calendar Module
Want to print a calendar for a full month?
import calendar
print(calendar.month(2025, 7))
Output:
July 2025
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Summary
Use the
time
module to get and format current or specific time.Use
calendar
to generate and display calendar views.You can convert between timestamps, human-readable formats, and structured time formats.
Keep this lesson in your toolbox—you’ll need it often when working with logs, delays, or time tracking in Python projects.