Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to calculate Date, Month and Year from Seconds
When working with timestamps, you often need to convert seconds (Unix epoch time) into readable date components. Python provides several approaches using the datetime module, time module, or manual calculations with divmod().
Using datetime Module
The datetime module offers classes to manipulate dates and times. The utcfromtimestamp() function converts seconds since epoch (January 1, 1970) into a datetime object.
Syntax
datetime.datetime.utcfromtimestamp(seconds)
Example
Here we convert 1706472809 seconds to extract the day, month, and year components ?
import datetime
def calculate_date(seconds):
date = datetime.datetime.utcfromtimestamp(seconds)
year = date.year
month = date.month
day = date.day
return day, month, year
seconds = 1706472809
day, month, year = calculate_date(seconds)
print("Day:", day, "Month:", month, "Year:", year)
Day: 28 Month: 1 Year: 2024
Using time Module
The time module provides the gmtime() function which converts seconds to a time tuple containing various time components including year, month, and day.
Syntax
time.gmtime(seconds)
Example
The gmtime() function returns a struct_time object with accessible attributes ?
import time
def calculate_date(seconds):
time_tuple = time.gmtime(seconds)
day = time_tuple.tm_mday
month = time_tuple.tm_mon
year = time_tuple.tm_year
return day, month, year
seconds = 1706472809
day, month, year = calculate_date(seconds)
print("Day:", day, "Month:", month, "Year:", year)
Day: 28 Month: 1 Year: 2024
Using divmod() Function
The divmod() function performs division and returns both quotient and remainder. This approach manually calculates the date by converting seconds to days and handling leap years.
Syntax
quotient, remainder = divmod(dividend, divisor)
Example
This method manually converts seconds to years, months, and days by accounting for leap years ?
def calculate_date(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
year = 1970
while days > 365:
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
if days > 366:
days -= 366
year += 1
else:
break
else:
days -= 365
year += 1
# Days in each month, accounting for leap year
month_days = [31, 28 + (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)),
31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month = 1
while days >= month_days[month - 1]:
days -= month_days[month - 1]
month += 1
return days + 1, month, year
seconds = 1706472809
day, month, year = calculate_date(seconds)
print("Day:", day, "Month:", month, "Year:", year)
Day: 28 Month: 1 Year: 2024
Comparison
| Method | Complexity | Best For |
|---|---|---|
| datetime module | Simple | Most common use cases |
| time module | Simple | When working with time tuples |
| divmod() function | Complex | Educational/custom requirements |
Conclusion
The datetime module's utcfromtimestamp() is the most straightforward approach for converting seconds to date components. Use the manual divmod() method only when you need to understand the underlying calculation process.
