patternpythonMinor
Messy Date Formatting
Viewed 0 times
formattingmessydate
Problem
There can only be better ways to do this?
- create strings for today's directory
strDateis yesterday's date in format ddmmyyyy (e.g. 08122016)
DateStris yesterday's date in format yymmdd (e.g 161208)
strDate = '{d.year}{d.month:02}{d.day:02}'.format(d=datetime.datetime.now()- timedelta(1))
strDate = str(strDate)
day = calendar.day_name[datetime.datetime.today().weekday()-1]
day = day[:3]
week = datetime.datetime.today().isocalendar()[1]
fpath = strDate + "_" + str(day) + "_" + str(week) + ".zip"
DateStr = '{d.day:02}{d.month:02}{d.year}'.format(d=datetime.datetime.now()- timedelta(1))
DateStr = str(DateStr[:4]+DateStr[6:])Solution
Formatting dates
Simpler way to produce your required the formats is using the strftime symbols, for example:
Formattings dates to use in filenames
I would strongly discourage using any of these formats in filenames.
Consider adopting the yyyymmdd format instead.
The good thing about this format is that the alphabetic ordering is the same as chronological ordering,
which is often very practical.
String conversions
The
So the second line here is unnecessary:
String concatenation
String concatenation like this is not recommended:
Better use the format function:
Other Python conventions
The
I suggest to read PEP8, it explains this and other recommendations to follow.
Simpler way to produce your required the formats is using the strftime symbols, for example:
# yesterday's date in format ddmmyyyy (e.g. 08122016)
strDate = '{:%d%m%Y}'.format(d - timedelta(1))
# yesterday's date in format yymmdd (e.g 161208)
DateStr = '{:%y%m%d}'.format(d - timedelta(1))Formattings dates to use in filenames
I would strongly discourage using any of these formats in filenames.
Consider adopting the yyyymmdd format instead.
The good thing about this format is that the alphabetic ordering is the same as chronological ordering,
which is often very practical.
String conversions
The
format function of a string returns a string.So the second line here is unnecessary:
strDate = '{d.year}{d.month:02}{d.day:02}'.format(d=datetime.datetime.now()- timedelta(1))
strDate = str(strDate)String concatenation
String concatenation like this is not recommended:
fpath = strDate + "_" + str(day) + "_" + str(week) + ".zip"Better use the format function:
fpath = '{}_{}_{}.zip'.format(strDate, day, week)Other Python conventions
The
strDate and DateStr variable names go against the recommended naming convention.I suggest to read PEP8, it explains this and other recommendations to follow.
Code Snippets
# yesterday's date in format ddmmyyyy (e.g. 08122016)
strDate = '{:%d%m%Y}'.format(d - timedelta(1))
# yesterday's date in format yymmdd (e.g 161208)
DateStr = '{:%y%m%d}'.format(d - timedelta(1))strDate = '{d.year}{d.month:02}{d.day:02}'.format(d=datetime.datetime.now()- timedelta(1))
strDate = str(strDate)fpath = strDate + "_" + str(day) + "_" + str(week) + ".zip"fpath = '{}_{}_{}.zip'.format(strDate, day, week)Context
StackExchange Code Review Q#149437, answer score: 6
Revisions (0)
No revisions yet.