python time.time()方法,获取时间戳
time.time()方法
python time模块中的time()方法,可以获取当前计算机系统基于当前时区的时间戳,即以林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数(基于当前时区),注意,时间单位为秒,与其它计算机语言的可能有所不同,比如JavaScript中一般用毫秒。需要将时间戳转换为日期,或将日期转化为时间戳,可以使用本站开发的转换器:时间戳和日期转换器在线。
语法
time.time()
python源码中对time()方法的介绍:
time() -> floating point number
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.
参数
无。若传递参数,python会抛出TypeError。
返回值
如python源码中的介绍:time()方法返回一个用python的浮点数类型表示的时间戳。注意,python的time.time()返回的时间戳的单位为秒,JavaScript的时间戳则一般以毫秒为单位。
time()实例代码
>>> import time
>>> date = time.time()
>>> type(date)
<class 'float'>
>>> date
1677823280.807006
>>> time.time(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: time() takes no arguments (1 given)