Python .localtime()
Published Oct 15, 2025
Contribute to Docs
In Python, the .localtime() method converts a time value, given in seconds since the epoch (January 1, 1970, 00:00:00 UTC), into local time. If no value is provided, it returns the current local time instead.
Syntax
time.localtime(seconds)
Parameters:
seconds(Optional): A number (floatorint) representing seconds since the epoch. Defaults to the current time if omitted.
Return value:
Returns a time.struct_time object representing the local time.
Example 1: .localtime() With No Argument
This example demonstrates when running .localtime() without the optional seconds argument:
import timelt = time.localtime()print('Current Local Time:', lt)
The output of the code is:
Current local time: time.struct_time(tm_year=2025, tm_mon=8, tm_mday=31, tm_hour=19, tm_min=53, tm_sec=36, tm_wday=6, tm_yday=243, tm_isdst=0)
Example 2: .localtime() With a Passed Argument
This example converts 5000 seconds since the epoch into local time:
import time# Passing the seconds elapsed as an argument to this methodlt = time.localtime(5000)print('Python Local Time:', lt)
The output of the code is:
Python local time: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=23, tm_sec=20, tm_wday=3, tm_yday=1, tm_isdst=0)
Codebyte Example: Using .localtime() to Log a File Download Time
This codebyte example demonstrates how to record the local time when a file download is completed:
Note: The output of
.localtime()may differ depending on the system clock, time zone settings, and daylight saving adjustments.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours