입력 : 2011-01-01 출력 : 2011-01-01 00:00:00
2011-01-01을 출력하는 방법은 무엇입니까?
# Packages
import datetime
def ObtainDate():
global d
isValid=False
while not isValid:
userInDate = raw_input("Type Date yyyy-mm-dd: ")
try: # strptime throws an exception if the input doesn't match the pattern
d = datetime.datetime.strptime(userInDate, '%Y-%m-%d')
isValid=True
except:
print "Invalid Input. Please try again.\n"
return d
print ObtainDate()
실제로 참조와 동일하지 않습니다. 나는 시간이 아닌 날짜만을 요구하고있다.
원하는 형식으로 구문 분석 된 개체의 형식을 지정하면됩니다.
d = datetime.datetime.strftime(datetime.datetime.strptime(userInDate, '%Y-%m-%d'), '%Y-%m-%d')
>>> d
'2015-05-09'
... 사실, 형식을 전혀 변경하지 않으려면 다음과 같이하십시오.
try: # strptime throws an exception if the input doesn't match the pattern
datetime.datetime.strptime(userInDate, '%Y-%m-%d')
except ValueError:
print "Invalid Input. Please try again.\n"
else:
isValid=True
d = userInDate
실제로 datetime
속도를 원한다면 완전히 건너 뛸 수 있습니다 .
if userInDate.replace('-','').isdigit() and len(userInDate) == 10 and userInDate[4] == userInDate[7] == '-':
d = userInDate
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다