Home > database >  (python flask) [Solved] getting date form URL then add and subtract days, having error response func
(python flask) [Solved] getting date form URL then add and subtract days, having error response func

Time:09-20

I'm working on getting the date value from URL input, can add/subtract days, showing "This is wrong input of date,dear customer", such as the input below

input:
http://.... /data?date=2022-01-01&diff=5&op={add,del}
http://.... /data?date=2022-01-01&diff=13&op=add

expect output:

The diff is: 5

The date is: 2022-01-01

The new_date is: 2022-01-14

from datetime import datetime, timedelta

from flask import Flask, request
from datetime import date,  datetime, time ,timedelta
# python testcodefmweb_clone.py
app = Flask(__name__)
 
@app.route('/data')
def query_example():
   
    date = request.args['date']
    diff = request.args['diff']

    diff_int = int(diff)
    diff_date = int(date)

    date_in_system = datetime.strptime( date,  "%Y-%m-%d")
   
    new_date = date_in_system   datetime.timedelta(days = diff_int)

    return '''          
              <h1>The diff is: {}</h1>
              <h1>The date is: {}</h1>
              <h1>The new_date is: {}'''.format(diff, date_in_system , new_date)
             
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

the website looks like for now (new_date calculation not working)

The python code picture

I search that the [try] [except] can help showing "This is wrong input of date, dear customer~"

now I'm stocking on putting the operation add,del work to add/subtract days, and showing error for wrong input

from datetime import datetime, timedelta
from flask import Flask, request
from datetime import date,  datetime, time ,timedelta
app = Flask(__name__)
 
@app.route('/data')
def query_example():
   
    date = request.args['date']
    diff = request.args['diff']

    diff_int = int(diff)
    
    date_in_system = datetime.strptime( date,  "%Y-%m-%d")
   
    new_date_add = date_in_system   timedelta(days = diff_int )
    new_date_minus = date_in_system - timedelta(days = diff_int )

    return '''          
              <h1>The diff is: {}</h1>
              <h1>The date is: {}</h1>
              <h1>The new_date_minus is: {}</h1>
              <h1>The new_date_add is: {}'''.format(diff, date_in_system , new_date_minus ,new_date_add)
             
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

(pic) the website looks like

working on putting [try] [except] for showing user error "sorry, wrong input dear customer~" in code

the third one putting [try] [except]that the website is not working
(pic) 3th code of website (not working)

from datetime import datetime, timedelta
from flask import Flask, request
from datetime import date,  datetime, time ,timedelta
app = Flask(__name__)
 
@app.route('/data')
def query_example():
   
    
        date = request.args['date']
        diff = request.args['diff']

        diff_int = int(diff)
        
        date_in_system = datetime.strptime( date,  "%Y-%m-%d")
    
        new_date_add = date_in_system   timedelta(days = diff_int )
        new_date_minus = date_in_system - timedelta(days = diff_int )


        try:
            datetime.datetime.strptime(date, "%Y-%m-%d")
            return '''          
                <h1>The diff is: {}</h1>
                <h1>The date is: {}</h1>
                <h1>The new_date_minus is: {}</h1>
                <h1>The new_date_add is: {}'''.format(diff, date_in_system , new_date_minus ,new_date_add)

        except ValueError:
            print("sorry, wrong input for date dear customer~")
          
             
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

solved! see the code below

import datetime
from datetime import datetime, timedelta
from flask import Flask, request
from datetime import date,  datetime, time ,timedelta
app = Flask(__name__)


@app.route('/data')

def query_example():
   
    try:
        date = request.args['date']
        diff = request.args['diff']
        op = request.args.get('op')
        
        
        diff_int = int(diff)
        
        date_in_system = datetime.strptime( date,  "%Y-%m-%d")
    
        new_date_add = date_in_system   timedelta(days = diff_int )
        new_date_minus = date_in_system - timedelta(days = diff_int )

        final_date = 0

        if op == "add":
            final_date = date_in_system   timedelta(days = diff_int )

        if op == "del":
            final_date = date_in_system - timedelta(days = diff_int )

   
    except Exception as e:
        return '''          
        
              <h1>捕捉錯誤資訊:  {}</h1>
              '''.format(diff  str(e))

        

        
    
    return '''          
              <h1>The diff is: {}</h1>
              <h1>The date is: {}</h1>
              <h1>The op is: {}</h1>
              
              <h1>The final_date is: {}'''.format(diff, date_in_system , op, final_date)
             
    
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

(pic)website showing to user the input is wrong

(pic)website for correct input result

CodePudding user response:

I think you need to change new_date = date_in_system datetime.timedelta(days = diff_int) to new_date = date_in_system timedelta(days = diff_int) since you import timedelta.

Is your expected output supposed to be:

The diff is: 5
The date is: 2022-01-01
The new_date is: 2022-01-06

This code snippet below should do what you want if the new date is 2022-01-06 and not 2022-01-14:

from datetime import date,  datetime, timedelta

date = "2022-01-01"
diff_int = 5

date_in_system = datetime.strptime( date,  "%Y-%m-%d")
new_date = date_in_system   timedelta(days = diff_int)

print(new_date)
  • Related