在 Python 中,你可以使用 `time` 模塊或 `datetime` 模塊來獲取毫秒級的時(shí)間戳。
方法一:使用 `time` 模塊獲取毫秒級時(shí)間戳
python
import time
# 獲取當(dāng)前時(shí)間的毫秒級時(shí)間戳
timestamp = int(time.time() * 1000)
print(timestamp)
方法二:使用 `datetime` 模塊獲取毫秒級時(shí)間戳
python
from datetime import datetime
# 獲取當(dāng)前時(shí)間
now = datetime.now()
# 轉(zhuǎn)換為毫秒級時(shí)間戳
timestamp = int(datetime.timestamp(now) * 1000)
print(timestamp)
以上兩種方法都可以獲取當(dāng)前時(shí)間的毫秒級時(shí)間戳。注意,時(shí)間戳是一個(gè)整數(shù),表示從某個(gè)固定時(shí)間點(diǎn)(通常是 1970 年 1 月 1 日)以來的毫秒數(shù)。
如果你需要在程序中多次獲取時(shí)間戳,建議將其封裝為一個(gè)函數(shù)以便重復(fù)使用。例如:
python
import time
def get_milliseconds_timestamp():
return int(time.time() * 1000)
# 使用函數(shù)獲取時(shí)間戳
timestamp = get_milliseconds_timestamp()
print(timestamp)
希望這可以幫助你獲取毫秒級的時(shí)間戳!