使用Python获取当前时间(以毫秒为单位)
Python中的时间模块提供了各种与时间相关的功能。该模块属于 Python 的标准实用程序模块,因此无需在外部安装它。
Time 模块的time.time()
方法用于获取自纪元以来的时间(以秒为单位)。闰秒的处理取决于平台。
注意:纪元是时间开始的点,并且取决于平台。在 Windows 和大多数 Unix 系统上,纪元是 1970 年 1 月 1 日 00:00:00 (UTC),并且闰秒不计入纪元以来的时间(以秒为单位)。要检查给定平台上的纪元,我们可以使用time.gmtime(0)
。
Syntax: time.time()
Return type: This method returns a float value which represents the time in seconds since the epoch.
示例 1:
# Python program to get
# time in milliseconds
# Import class time from time module
from time import time
# time() function is multiplied with
# 1000 to convert seconds into milliseconds.
milliseconds = int(time() * 1000)
print("Time in milliseconds since epoch", milliseconds)
输出:
Time in milliseconds since epoch 1576071104408
示例 2:使用 lambda函数
# Python program to get
# time in milliseconds
# Import class time from time module
from time import time
# Get time in milliseconds using
# lambda function
milliseconds = lambda: int(time() * 1000)
print("Time in milliseconds since epoch", milliseconds())
输出:
Time in milliseconds since epoch 1576071316487