I want to input the date in flask and can calculate the date(add to 3 days later, or not let system take 2022-02-32 input from user), I searched that we can use the function below but what do I missed to correct?
Here is the function I start with, I thought could help:
from datetime import date
date = datetime.datetime.strptime(date, "%m/%d/%y")
new_date = date + datetime.timedelta(days= diff)
python flask input URL (date,diff, operation-add,del) how can I correct
example URL input:
http://127.0.0.1:5000/data?dtae=2022-01-01&diff=5&op=a{add,del}
http://127.0.0.1:5000/data?dtae=2022-01-01&diff=13&op=add
http://127.0.0.1:5000/data?dtae=2022-01-01&diff=5&op=del
optput:
the 13 days later is 2022-01-14 the 5 days back is 2021-12-27
# import main Flask class and request object
from flask import Flask, request
from datetime import date, datetime, time ,timedelta
# create the Flask app
app = Flask(__name__)
@app.route('/data')
def query_example():
# if key doesn't exist, returns None
name = request.args.get('name')
# if key doesn't exist, returns a 400, bad request error
CustomerID = request.args['CustomerID']
# if key doesn't exist, returns None
date = request.args.get('date')
diff = request.args.get('diff')
# take input from string to other variable變數
CustomerID_float = float(CustomerID)
CustomerID_type = type(CustomerID)
name_type = type(name)
print(type(name))
date = datetime.datetime.strptime(date, "%m/%d/%y")
new_date = date + datetime.timedelta(days= diff)
new_date = date
return '''
<h1>The name is: {}</h1>
<h1>The CustomerID is: {}</h1>
<h1>The CustomerID float is: {}</h1>
<h1>The date is: {}</h1>
<h1>The new_date is: {}'''.format(name, CustomerID, CustomerID_float, date , new_date)
if __name__ == '__main__':
# run app in debug mode on port 5000
app.run(debug=True, port=5000)