Python: Date Time Reformatter

Python Random

A simple function that takes a DateTime in the format: 2023-11-29T06:41:20-08:00 and outputs it in a much more pretty: 29/11/2023 06:41:20 -0800.

As you can see its switching to the DDMMYY date format, followed by the time and finally the timezone.

#!/usr/bin/env python
from datetime import datetime

def F_prettyDateTime(strlongdate):
   date_format = '%Y-%m-%dT%H:%M:%S%z'
   date_obj = datetime.strptime(strlongdate,date_format)
   return date_obj.strftime('%d/%m/%Y %H:%M:%S %z')

F_prettyDateTime("2023-11-29T06:41:20-08:00")

Leave a Reply

Your email address will not be published. Required fields are marked *