Compilador de Python Online (Editor / Interpretador)
Escreva, execute e compartilhe o seu código Python instantaneamente com o Compilador de Python Online da Asimov Academy.
import time
from datetime import datetime, date
# Analisar uma string em um objeto struct_time
struct_time = time.strptime('Jan 1 2023 4:23PM', '%b %d %Y %I:%M%p')
# Converter struct_time em um timestamp
timestamp = time.mktime(struct_time)
# Converter timestamp em um objeto datetime
objeto_datetime = datetime.fromtimestamp(timestamp)
print(objeto_datetime) # Saída: datetime.datetime(2023, 1, 1, 16, 23)
# Converter timestamp em um objeto date
objeto_date = date.fromtimestamp(timestamp)
print(objeto_date) # Saída: date(2023, 1, 1)