📜  Python| time.asctime() 方法

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

Python| time.asctime() 方法

Python时间方法 time.asctime()用于将表示time.gmtime()time.localtime()方法返回的时间的元组或time.struct_time对象转换为以下形式的字符串:

Day Mon Date Hour:Min:Sec Year
For example:
Thu 08 22 10:46:56 2019

Python时间 asctime() 语法:

Python时间 asctime()参数:

Python时间 asctime()返回类型:

Python时间 asctime() 示例

示例 1:使用 time.asctime() 方法

Python3
# Python program to explain time.asctime() method
 
# importing time module
import time
 
 
# Convert the current time
# as returned by time.time() method
# to a time.struct_time object in UTC
# using time.gmtime() method
obj = time.gmtime()
 
# Print time.struct_time object
print("time.struct_time object as returned by time.gmtime() method:")
print(obj)
 
# Convert the time.struct_time
# object to a string of the
# form 'Day Mon Date Hour:Min:Sec Year'
# using time.asctime() method
time_str = time.asctime(obj)
 
# Print the time.struct_time object
# to the string of the
# form 'Day Mon Date Hour:Min:Sec Year'
print("\ntime.struct_time obj in string of the form \
'Day Mon Date Hour:Min:Sec Year':")
print(time_str)
 
 
# Convert the current time
# as returned by time.time() method
# to a time.struct_time object in local time
# using time.localtime() method
obj = time.localtime()
 
# Print time.struct_time object
print("\ntime.struct_time object as returned by \
time.localtime() method:")
print(obj)
 
# Convert the time.struct_time
# object to a string of the
# form 'Day Mon Date Hour:Min:Sec Year'
# using time.asctime() method
time_str = time.asctime(obj)
 
# Print the time.struct_time object
# to the string of the
# form 'Day Mon Date Hour:Min:Sec Year'
print("\ntime.struct_time obj in string of the form \
'Day Mon Date Hour:Min:Sec Year':")
print(time_str)


Python3
# Python program to explain time.asctime() method
 
# importing time module
import time
 
 
# If the parameter 't'
# in time.asctime() method
# is not provided then
# the current time as
# returned by time.localtime()
# method is used
 
# Convert the current time
# as returned by time.localtime() method
# to a string of the
# form 'Day Mon Date Hour:Min:Sec Year'
# using time.asctime() method
time_str = time.asctime()
 
# Print the string
print(time_str)


Python3
# Python program to explain time.asctime() method
 
# importing time module
import time
 
 
# A tuple with 9 attributes
# representing a time
t = (2019, 8, 22, 11, 21, 48, 3, 234, 0)
 
# Convert the tuple
# to a string of the
# form 'Day Mon Date Hour:Min:Sec Year'
# using time.asctime() method
time_str = time.asctime(t)
 
# Print the string
print(time_str)


输出:

示例 2:如果未提供参数 't'

Python3

# Python program to explain time.asctime() method
 
# importing time module
import time
 
 
# If the parameter 't'
# in time.asctime() method
# is not provided then
# the current time as
# returned by time.localtime()
# method is used
 
# Convert the current time
# as returned by time.localtime() method
# to a string of the
# form 'Day Mon Date Hour:Min:Sec Year'
# using time.asctime() method
time_str = time.asctime()
 
# Print the string
print(time_str)

输出:

Thu Oct  7 21:01:45 2021

示例 3:如果参数 't' 是一个元组

Python3

# Python program to explain time.asctime() method
 
# importing time module
import time
 
 
# A tuple with 9 attributes
# representing a time
t = (2019, 8, 22, 11, 21, 48, 3, 234, 0)
 
# Convert the tuple
# to a string of the
# form 'Day Mon Date Hour:Min:Sec Year'
# using time.asctime() method
time_str = time.asctime(t)
 
# Print the string
print(time_str)

输出:

Thu Aug 22 11:21:48 2019