标签云

微信群

扫码加入我们

WeChat QR Code

Input is: 2011-01-01Output is: 2011-01-01 00:00:00How do it get it to output to be: 2011-01-01 ??# Packagesimport datetimedef ObtainDate():global disValid=Falsewhile not isValid:userInDate = raw_input("Type Date yyyy-mm-dd: ")try: # strptime throws an exception if the input doesn't match the patternd = datetime.datetime.strptime(userInDate, '%Y-%m-%d')isValid=Trueexcept:print "Invalid Input. Please try again.\n"return dprint ObtainDate()actually not the same as the reference.I'm asking just for the date not the time.


possible duplicate of Converting a string to a formatted date-time string using Python

2019年04月20日25分51秒

You can format the output just like you formatted the input. Change a single letter (strptime to strftime) and you're 99% of the way there.

2019年04月19日25分51秒

doing that gives me an invalid input.

2019年04月20日25分51秒

Actually, you can just try the parse and, if successful, assign d = userInDate, since you just wanted to check whether the input was valid (added to answer).

2019年04月20日25分51秒