convert a unix timestamp to a date in python

To convert a Unix timestamp to a date in Python, you can use the datetime module.

main.py
import datetime

# Unix timestamp
timestamp = 1635461070

# Convert Unix timestamp to datetime object
date = datetime.datetime.fromtimestamp(timestamp)

# Format the datetime object as a string
formatted_date = date.strftime('%Y-%m-%d %H:%M:%S')

# Print the date in the desired format
print(formatted_date)
308 chars
14 lines

This will output the date in the format of YYYY-MM-DD HH:MM:SS. You can change the format by modifying the strftime argument to suit your needs.

gistlibby LogSnag