파이썬으로 지난 달의 날짜를 얻으려고합니다. 내가 시도한 것은 다음과 같습니다.
str( time.strftime('%Y') ) + str( int(time.strftime('%m'))-1 )
그러나이 방법은 두 가지 이유로 나쁩니다. 먼저 2012 년 2 월 (201202 대신)에 20122를 반환하고 두 번째로 1 월에 12 대신 12 대신 0을 반환합니다.
나는이 문제를 bash에서 해결했다.
echo $(date -d"3 month ago" "+%G%m%d")
bash 가이 목적을 위해 내장 된 방법을 가지고 있다면 훨씬 더 많은 파이썬 이이 목표를 달성하기 위해 자신의 스크립트를 작성하는 것보다 더 나은 것을 제공해야한다고 생각합니다. 물론 나는 다음과 같은 것을 할 수있다 :
if int(time.strftime('%m')) == 1:
return '12'
else:
if int(time.strftime('%m')) < 10:
return '0'+str(time.strftime('%m')-1)
else:
return str(time.strftime('%m') -1)
이 코드를 테스트하지 않았으며 어쨌든 사용하고 싶지 않습니다 (다른 방법을 찾을 수 없다면 : /)
당신의 도움을 주셔서 감사합니다!