python怎么获取当前时间戳

2020/09/27 07:12

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

推荐课程:Python新手教程(Corey Schafer)

免费直播

    精选课程 更多

    注册电脑版

    版权所有 2003-2020 广州环球青藤科技发展有限公司