PythonDatetime.date类的fromtimestamp()函数
fromtimestamp()函数用于返回指定时间戳对应的日期。
注意:这里的时间戳范围从 1970 年到 2038 年,如果时间戳中存在闰秒,则此函数不考虑闰秒。这个函数是一个类方法。
Syntax: @classmethod fromtimestamp(timestamp)
Parameters: This function accepts a parameter which is illustrated below:
- timestamp: This is the specified timestamp for which the date is going to be returned.
Return values: This function returns the date corresponding to a specified timestamp.
示例 1:获取与 Epoch 和 Unix 时间戳对应的日期。
Python3
# Python3 code to demonstrate
# Getting a date corresponding
# to a specified timestamp
# Importing datetime and time module
import datetime
import time
# Calling the time() function
# to return current time
Todays_time = time.time()
# Printing today's time
print(Todays_time)
# Calling the fromtimestamp() function
# to get date from the current time
date_From_CurrentTime = datetime.date.fromtimestamp(Todays_time);
# Printing the current date
print("Date for the Timestamp is: %s"%date_From_CurrentTime);
Python3
# Python3 code to demonstrate
# Getting a date corresponding
# to a specified timestamp
# Importing datetime and time module
import datetime
import time
# Initializing a timestamp value
Timestamp = 1323456464;
# Calling the fromtimestamp() function
# over the above specified Timestamp
date_From_Timestamp = datetime.date.fromtimestamp(Timestamp);
# Printing the date
print("Date for the Timestamp is: %s"%date_From_Timestamp);
输出:
1627279008.95
Date for the Timestamp is: 2021-07-26
示例 2:获取与指定时间戳对应的日期。
蟒蛇3
# Python3 code to demonstrate
# Getting a date corresponding
# to a specified timestamp
# Importing datetime and time module
import datetime
import time
# Initializing a timestamp value
Timestamp = 1323456464;
# Calling the fromtimestamp() function
# over the above specified Timestamp
date_From_Timestamp = datetime.date.fromtimestamp(Timestamp);
# Printing the date
print("Date for the Timestamp is: %s"%date_From_Timestamp);
输出:
Date for the Timestamp is: 2011-12-09