fromtimestamp() Function Of Datetime.date Class In Python

The fromtimestamp() method returns the date corresponding to a given timestamp. The fromtimestamp() method is defined as part of the date class. The Python example returns date from current timestamp, as well as from the Unix Epoch.

Date Updated 12 Jul 2019 1 min read

Function Name

fromtimestamp

Function Signature

@classmethod fromtimestamp(timestamp)

Parameters

timestamp – The timestamp for which the date needs to be computed.

Return Value

Returns the date corresponding to a valid timestamp.

Function Overview

  • The method fromtimestamp() of the date class computes the date corresponding to a given timestamp and returns it. Methods like time.time() can be used get a timestamp.

Note

  • The supported timestamp range is from the year 1970 to the year 2038.
  • The fromtimestamp() method does not consider leap seconds if any present in the timestamp.
  • The method fromtimestamp() is a class method.

Exceptions Raised

OverflowError, OSError

Example

python
# ----- Example Python program to get date from timestamp -----

import datetime

import time

# Get date from current time

dateFromCurrentTime = datetime.date.fromtimestamp(time.time())

print("Date:(Timestamp used is current time):%s"%dateFromCurrentTime)

# Get date from start of unix epoch

dateFromCurrentTime = datetime.date.fromtimestamp(0)

print("Date:(Timestamp used is Unix Epoch time):%s"%dateFromCurrentTime)

Output

Output
Date:(Timestamp used is current time):2019-07-12

Date:(Timestamp used is Unix Epoch time):1970-01-01