from datetime import date
today = dt.datetime.today().date()
todays_file = 'C:\\Temp\\'+str(today.year)+str(today.month)+str(today.day)+str('.csv')
运行:
todays_file
给予:
'C:\\Temp\\2018124.csv'
但是-我需要使用yyyymmdd格式(不是yyyymmd)
所以预期的输出是:
'C:\\Temp\\20181204.csv'
类似地,2019年2月3日的预期输出是:
'C:\\Temp\\20190203.csv'
有没有使用len(today.day或Today.month)= 1的聪明方法吗?
答案 0 :(得分:1)
您可以这样做:
const { state } = this.props.location;
if (state && state.copySurveyId) {
this.duplicateSurvey(state.copySurveyId);
const stateCopy = { ...state };
delete stateCopy.copySurveyId;
this.props.history.replace({ state: stateCopy });
}
给予:
d = datetime.datetime.now()
todays_file = 'C:\\Temp\\' + str(d.year) + str("%02d"%d.month) + str("%02d"%d.day) + '.csv'
答案 1 :(得分:0)
使用.strftime("%Y%m%d")
例如:
import datetime
today = datetime.datetime.today().date()
todays_file = r'C:\\Temp\\{}.csv'.format(today.strftime("%Y%m%d"))
print(todays_file)
输出:
C:\\Temp\\20181204.csv
您还可以使用str.format
或fstring
形成所需的结果字符串。
答案 2 :(得分:0)
from datetime import date
today_str = date.today().strftime('%Y%m%d')
file_name = '{}.csv'.format(today_str)
# or in python 3.6+
file_name = f'{today_str}.csv'