Converting Epoch time into the datetime
⚡TLDR
One-liner to convert epoch time to datetime in Python using datetime.fromtimestamp()
:
Accounting for Time Zones
While dealing with epoch time, remember to factor in time zones. For UTC conversion, simply switch to utcfromtimestamp()
:
Guard against TypeError
by ensuring your epoch time is a float or an int:
String Formatting the DateTime
Yearning for a custom datetime format? Leverage Python's strftime
:
Ensuring Meaningful Input
Watch out for functions fromtimestamp()
as they're picky about what input data types:
Working with MySQL
In SQL-land, convert epoch time to MySQL time using FROM_UNIXTIME
:
Need just the hour from epoch time? Here's how:
Epoch Time of Present
Get the current epoch time in a jiffy:
Linked
Linked
Was this article helpful?