2018-01-24 chp07的date变量 ,如何做validation,程序改进

The slightly counterintuitive workaround is to convert the string input we receive

from the user to a datetime object and then back to a string (which will always be in

the correct format) before passing it into our database code to store in MySQL.


First off, add the following helper function to your crimemap.py file:


def format_date(userdate):

date = dateparser.parse(userdate)        --// 旁白: 先转成 date-object   

try:

return datetime.datetime.strftime(date, "%Y-%m-%d")   -- // 果然如上面 所言,从 date-object 再转成  字符型 


注意 :  datetime.datetime.strftime (striFtime)是经典的  

由字符串转为日期型的函数为:datetime.datetime.strptime(),

你可能感兴趣的:(2018-01-24 chp07的date变量 ,如何做validation,程序改进)