python time.asctime()方法,格式化时间
time.asctime()方法
python time模块的asctime()方法,可以将结构化的时间转化为“Sat Jun 06 16:26:11 2022”格式化的时间字符串。
语法
time.asctime( [tuple] )
python源码中对asctime()方法的介绍:
asctime([tuple]) -> string
Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
When the time tuple is not present, current time as returned by localtime()
is used.
参数
参数 | 描述 |
---|---|
无 | 不传递参数的情况,即the time tuple is not present,asctime()方法将默认使用当前的日期时间来转换为对应格式的字符串——current time as returned by localtime() is used. |
tuple | 可选参数,为元组,比如localtime()的返回类型。 |
返回值
格式化的时间字符串。
asctime()实例代码
>>> import time
>>> time.asctime()
'Sat Mar 4 16:57:49 2023'
>>> time.asctime(time.localtime())
'Sat Mar 4 16:58:01 2023' #之所以与上面的多了几秒,是因为输入代码和运行代码的时间