當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python time.asctime()用法及代碼示例


Python中的時間模塊提供了各種與時間相關的函數。該模塊屬於Python的標準實用程序模塊。

time.asctime()時間模塊的方法用於轉換元組或time.struct_time代表時間的對象time.gmtime()或者time.localtime()方法,其格式如下:

Day Mon Date Hour:Min:Sec Year

For example:
Thu 08 22 10:46:56 2019
用法: time.asctime([t])

參數:
t(可選):一個元組或time.struct_time對象,表示由time.gmtime()方法或time.localtime()方法返回的時間。如果未提供“ t”參數或“無”,則使用time.localtime()方法返回的當前時間。

返回類型:此方法返回形式為Day Mon Date Hour:Min:Sec Year的字符串

代碼1:用於time.asctime()方法

# 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)
輸出:
time.struct_time object as returned by time.gmtime() method:
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=22, tm_hour=5, tm_min=36,
tm_sec=37, tm_wday=3, tm_yday=234, tm_isdst=0)

time.struct_time obj in string of the form 'Day Mon Date Hour:Min:Sec Year':
Thu Aug 22 05:36:37 2019

time.struct_time object as returned by time.localtime() method:
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=22, tm_hour=11, tm_min=12,
tm_sec=41, tm_wday=3, tm_yday=234, tm_isdst=0)

time.struct_time obj in string of the form 'Day Mon Date Hour:Min:Sec Year':
Thu Aug 22 11:12:41 2019

代碼2:如果未提供參數“ t”

# 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 Aug 22 11:17:22 2019

代碼3:如果參數“ t”是一個元組

# Python program to explain time.asctime() method 
  
# importing time module 
import time 
  
  
# A tuple with 9 attributes 
# represnting 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

參考: https://docs.python.org/3/library/time.html#time.asctime



相關用法


注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | time.asctime() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。