首页 > IT职业 > IT图书 > python怎么获取当前时间戳
python获取当前时间戳的方法:1、使用time模块,语法为“time.time()”;2、使用datetime模块,语法为“datetime.datetime.now().timestamp()”。

使用模块time
import time now = time.time() print(now) 1593580247.232345
使用模块datetime
模块datetime提供了以更面向对象的方式操作日期和时间的类。其中之一是date .datetime。现在返回历元之后的秒数。
import datetime; ts = datetime.datetime.now().timestamp() print(ts) 1593580361.14386
使用模块calendar
获取当前时间戳的另一种方法是从多个模块组合多个函数。Python还提供calendar模块。在本例中,我们将使用函数calendar。转换表示当前时间的元组。
import calendar; import time; ts = calendar.timegm(time.gmtime()) print(ts) 1593580437
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3370/viewspace-2832950/ ,如需转载,请注明出处,否则将追究法律责任。