用time模块获取时间
import time
print(time.time())
1586813438.419919
print(time.ctime())
Mon Apr 13 23:30:38 2020
用datetime模块获取时间
import datetime
print(datetime.datetime.now())
2021-11-13 23:30:38.419951
print(datetime.date.today())
2021-11-13
用os模块获取时间
import os
os.system(‘date’)
Sun Feb 20 10:12:36 UTC 2022
os.system(‘date +"%Y-%m-%d %H:%M:%S"’)
2022-02-20 10:30:09
3 ways to get the current time in python
10 tips to use Python OS module
本文介绍了Python中使用time、datetime和os模块获取当前时间的方法。通过time.time()获取时间戳,time.ctime()转换为易读格式,以及利用datetime.datetime.now()和datetime.date.today()获取详细和日期。此外,还展示了如何通过os.system调用系统命令获取日期。

1万+

被折叠的 条评论
为什么被折叠?



