Python获取当前时间戳的方法:1、使用time模块,语法为“time.time()”;2、使用datetime模块,语法为“datetime.datetime.now().timestamp()”。
使用模块time
importtime now=time.time() print(now) 1593580247.232345
使用模块datetime
模块datetime提供了以更面向对象的方式操作日期和时间的类别。其中之一是date .datetime。现在返回历元后的秒数。
importdatetime; ts=datetime.datetime.now().timestamp() print(ts) 1593580361.14386
模块calendar使用模块
获取当前时间戳的另一种方法是从多个模块组合多个函数。Python还提供calendar模块。在这种情况下,我们将使用函数calendar。转换表示当前时间的元组。
importcalendar; importtime; ts=calendar.timegm(time.gmtime()) print(ts) 1593580437
推荐课程:Python新手教程(Corey Schafer)