Python
# Example using strftime
# Example using strptime
date_str = '2023-04-01 14:30:00'
dt = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
print(dt)  # Output: 2023-04-01 14:30:00


from datetime import datetime
dt = datetime(2023, 4, 1, 14, 30, 0)
print(dt.strftime('%Y-%m-%d %H:%M:%S'))  # Output: 2023-04-01 14:30:00
print(dt.strftime('%b %d, %Y %I:%M %p'))  # Output: Apr 01, 2023 02:30 PM

DirectiveMeaningExample
%YYear (4 digits)datetime.datetime.now().strftime("%Y") returns '2023'
%mMonth (zero-padded)datetime.datetime.now().strftime("%m") returns '04'
%dDay of the month (zero-padded)datetime.datetime.now().strftime("%d") returns '01'
%HHour (24-hour clock)datetime.datetime.now().strftime("%H") returns '13'
%MMinute (zero-padded)datetime.datetime.now().strftime("%M") returns '42'
%SSecond (zero-padded)datetime.datetime.now().strftime("%S") returns '57'
%aAbbreviated weekday namedatetime.datetime.now().strftime("%a") returns 'Fri'
%AFull weekday namedatetime.datetime.now().strftime("%A") returns 'Friday'
%bAbbreviated month namedatetime.datetime.now().strftime("%b") returns 'Apr'
%BFull month namedatetime.datetime.now().strftime("%B") returns 'April'
%cLocale’s date and timedatetime.datetime.now().strftime("%c") returns 'Fri Apr 1 13:42:57 2023'
%xLocale’s datedatetime.datetime.now().strftime("%x") returns '04/01/23'
%XLocale’s timedatetime.datetime.now().strftime("%X") returns '13:42:57'
CodeMeaningExample
%YYear with century as a decimal number2023
%mMonth as a zero-padded decimal number04
%dDay of the month as a zero-padded decimal number01
%HHour (24-hour clock) as a zero-padded decimal number14
%MMinute as a zero-padded decimal number30
%SSecond as a zero-padded decimal number00
%bMonth abbreviationApr
%BFull month nameApril
%aWeekday abbreviationFri
%AFull weekday nameFriday
%pAM or PMPM
%zUTC offset in the form +HHMM or -HHMM+0530
%ZTime zone nameIST