fromtimestamp() Function Of Datetime.date Class in Python

The fromtimestamp() function of the Python datetime.date class converts a Unix timestamp into a date object. A timestamp represents the duration since the epoch (January 1, 1970, 00:00:00 UTC). This function is particularly useful when working with databases, log files, or APIs that store dates as timestamps.

Syntax

datetime.date.fromtimestamp(timestamp)

The method takes a single parameter the timestamp value in seconds and returns a date object. Since it's a class method, you must call it using the class name datetime.date.

Basic Example

import datetime

# Create a timestamp value (January 1, 2021, 00:00:00 UTC)
timestamp = 1609459200
date_obj = datetime.date.fromtimestamp(timestamp)

print("Timestamp:", timestamp)
print("Date object:", date_obj)
print("Type:", type(date_obj))
Timestamp: 1609459200
Date object: 2021-01-01
Type: <class 'datetime.date'>

The timestamp 1609459200 corresponds to January 1, 2021. The fromtimestamp() method extracts the year, month, and day from this timestamp and creates a date object.

Practical Example: Financial Transaction Summary

Here's a real-world scenario where you process financial transactions with Unix timestamps to create a daily summary report ?

import pandas as pd
import datetime

# Mock dataframe with transaction data
data = {
    'timestamp': [1609459200, 1609459200, 1609545600, 1609545600, 1609632000], 
    'amount': [100.0, 200.0, 300.0, 400.0, 500.0]
}
df = pd.DataFrame(data)

# Dictionary to accumulate transaction amounts by date
daily_totals = {}

# Process each transaction
for index, row in df.iterrows():
    date = datetime.date.fromtimestamp(row['timestamp'])
    daily_totals[date] = daily_totals.get(date, 0) + row['amount']

# Print summary report
print('Date\t\tTotal Amount')
print('------------------------')
for date, total in sorted(daily_totals.items()):
    print(f'{date}\t${total:,.2f}')
Date		Total Amount
------------------------
2021-01-01	$300.00
2021-01-02	$700.00
2021-01-03	$500.00

Working with Current Timestamp

You can also get the current date using the current timestamp ?

import datetime
import time

# Get current timestamp
current_timestamp = time.time()
current_date = datetime.date.fromtimestamp(current_timestamp)

print(f"Current timestamp: {current_timestamp:.0f}")
print(f"Current date: {current_date}")
Current timestamp: 1704067200
Current date: 2024-01-01

Common Use Cases

Use Case Description Example Source
Database Records Convert stored timestamps to readable dates User registration dates
Log File Processing Extract dates from log entries Server access logs
API Responses Parse timestamp fields from JSON Social media posts
Data Analysis Group time-series data by date Financial transactions

Key Points

  • Returns a datetime.date object, not a datetime.datetime object

  • Timestamp must be in seconds (not milliseconds)

  • Uses local timezone by default

  • Class method called on the class, not an instance

Conclusion

The fromtimestamp() method is essential for converting Unix timestamps into readable date objects in Python. It's particularly useful for data processing, log analysis, and working with APIs that return timestamp values.

Updated on: 2026-03-27T01:16:12+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements