Home > Net >  Custom datetime formating Python
Custom datetime formating Python

Time:11-05

I want to create custom format date and i'm struggling with that, need help.
Is it possible to create datetime format like this "02FEB2021 00:00:00" in Python ?

CodePudding user response:

Yes with a custom formatter

from datetime import datetime

fmt = "%d%b%Y %H:%M:%S"

date_str = datetime.now().strftime(fmt).upper()
print(date_str)  # 04NOV2021 19:07:50

date = datetime.strptime(date_str, fmt)
print(date)  # 2021-11-04 19:07:50
  • Related