I am trying to get a date formatted, i.e., from Jan
to JA
, Feb
to FE
. I have a dictionary built to do this, however my code only works randomly, e.g., sometimes every 4 times, sometimes every 24 times. I am new to Python, and I can't make sense of this. The output from the below code is 16Jun28
most of the time, but correctly 16JN28
sometimes.
import datetime
month_code = {'Jan': 'JA',
'Feb': 'FE',
'Mar': 'MR',
'Apr': 'AL',
'May': 'MA',
'Jun': 'JN',
'Jul': 'JL',
'Aug': 'AU',
'Sep': 'SE',
'Oct': 'OC',
'Nov': 'NO',
'Dec': 'DE'}
today = datetime.datetime.now()
DD = datetime.timedelta(days=90)
use_by = today + DD
use_by_str = use_by.strftime("%y-%b-%d")
def label_function():
month = use_by.strftime("%b")
year = use_by.strftime("%y")
day = use_by.strftime("%d")
return year + month + day
line = label_function()
for k, v in month_code.items():
Result = line.replace(k, v)
print(Result)