データをクエリするためにcurrent_month変数を使用しますが、ここではキャッチがあります。月の日が15日後であれば、現在の月を翌月とする。したがって、2011年4月16日の現在の月は2011年5月1日である必要があります。私は動作するコードを持っていますが、pythonicを感じません。提案をいただければ幸いです。15日後の次の月にロールオーバーする現在の月変数を調べる - python 2.7
month = datetime.datetime.now().strftime("%m")
year = datetime.datetime.now().strftime("%Y")
day = datetime.datetime.now().strftime("%d")
#Check if day in the month is past 15th if so set current month
if int(day) > 15:
if int(month) < 9: # check if the month in 1-9 if so pad leading zero
x = int(month)+1
current_month = year+"-0"+str(x)+"-01"
if int(month) == 9: # check if the month in 1-9 if so pad leading zero
x = int(month)+1
current_month = year+"-"+str(x)+"-01"
elif int(month) == 12: # check if the month is Dec if so roll to the next year and set month to Jan
month = "01"
y = int(year)+1
current_month = str(y)+"-"+month+"-01"
else:
x = int(month)+1 # just add one to the month if months are 10 or 11
current_month = year+"-"+str(x)+"-01"
else:
current_month = year+"-"+month+"-01" #prior to the 15'th so just use normal year, month and day
現在の日付に16日を追加して1日に設定することはできませんか? –
@ScottHunter:月の数が異なるため、日数を加算することが不正確になることがあります。 15日を引いた後、1日を1にして1ヶ月を追加すると正確になります。 – zondo
@zondo:調整が必要な場合にのみ行うか、 –