Python (Scripting) - epoch date and time
by
Jeremy Canfield |
Updated: April 01 2024
| Python (Scripting) articles
time can be used to return epoch time, which is the number of seconds that have elapsed since January 1st 1970 on a Linux system.
#!/usr/bin/python3
import time
print(f"current epoch = {time.time()}")
Something like this should be returned. In this example, 1701837790 seconds have elapsed since January 1st, 1970.
current epoch = 1701837790.605019
datetime can be used to convert epoch into a datetime object.
#!/usr/bin/python3
import time
from datetime import datetime
print(f"current epoch = {time.time()}")
datetime_object = datetime.fromtimestamp(time.time())
print(f"datetime_object = {datetime_object}")
Something like this should be returned.
current epoch = 1701837790.605019
datetime_object = 2023-12-05 22:34:44.592326
Did you find this article helpful?
If so, consider buying me a coffee over at