📜  PythonDatetime.date类的fromtimestamp()函数

📅  最后修改于: 2022-05-13 01:55:00.425000             🧑  作者: Mango

PythonDatetime.date类的fromtimestamp()函数

fromtimestamp()函数用于返回指定时间戳对应的日期。

注意:这里的时间戳范围从 1970 年到 2038 年,如果时间戳中存在闰秒,则此函数不考虑闰秒。这个函数是一个类方法。

示例 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