ใช้ datetime Module แปลง Datetime เป็น UTC Timestamp หากมี Datetime Object ใน UTC แล้ว ใช้ timestamp() ได้ UTC Timestamp (เวลาตั้งแต่ Epoch)
ตัวอย่าง Python 3
from datetime import datetime, timezone dt = datetime(2015, 10, 19) timestamp = dt.replace(tzinfo=timezone.utc).timestamp() print(timestamp)
ผลลัพธ์
1445212800.0
Python 2: ใช้ total_seconds()
from datetime import datetime dt = datetime(2015, 10, 19) timestamp = (dt - datetime(1970, 1, 1)).total_seconds() print(timestamp)
ผลลัพธ์
1445212800.0