Nikhil Vyas

Developer, Gamer. Python, C++, Rimworld

Simple way to epoch

25 Aug 2018 » python

Came across this question recently: stackoverflow: Convert a column of yyyy-mm-dd datetimes to linux time epoch in Python

Which referenced this question: stackoverflow: Convert a column of datetimes to epoch in Python

The accepted answer(s) suggests to find the diff between the date with the epoch reference date and calculate the seconds.

Well, we can… do this. There’s a reason we’re using the datetime objects and not writing our own classes for datetime.

from datetime import datetime

dt = datetime.now()
print(f"{dt.timestamp()}")

Just use .timestamp() on your datetime object, people. That’s it.

import this -> Rule:3 -> Simple is better than complex.