Convert date to timestamp and timestamp to date in Python

From PedrosBrainDump
Revision as of 11:17, 6 February 2025 by 413vhcu1lq0463ob (talk | contribs) (Created page with "Date to timestamp from datetime import datetime # Date date = datetime(2025, 2, 6) # Example date (Year, Month, Day) # Convert to timestamp timestamp = date.timestamp() print(timestamp) Timestamp to date from datetime import datetime # Timestamp timestamp = 1615931732 # Example timestamp # Convert to date date = datetime.utcfromtimestamp(timestamp).date() print(date)")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Date to timestamp

from datetime import datetime

# Date
date = datetime(2025, 2, 6)  # Example date (Year, Month, Day)

# Convert to timestamp
timestamp = date.timestamp()

print(timestamp)

Timestamp to date

from datetime import datetime
# Timestamp
timestamp = 1615931732  # Example timestamp 

# Convert to date
date = datetime.utcfromtimestamp(timestamp).date() 
print(date)