Fromtimestamp() Function Of Datetime.date Class In Python

Function Name:

fromtimestamp

Function Signature:

@classmethod fromtimestamp(timestamp)

 

Parameters:

timestampThe 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:

# ----- 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:

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

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


Copyright 2024 © pythontic.com