I have read through the relative question in the following, but I'm not sure why the code is not working when I tried to put the concept together
Get the number of week days with date between two dates in python [Stack overflow]:
Get the number of week days with date between two dates in python
Number of days between 2 dates, excluding weekends [Stack overflow]:
Number of days between 2 dates, excluding weekends
The input of URL:
http://127.0.0.1:5000/data?date1=2022,03,06|00:00:00&date2=2022,03,08|00:00:00
The error code:
import datetime
from datetime import datetime, timedelta
from flask import Flask, request
from datetime import date, datetime, time ,timedelta
import pandas as pd
app = Flask(__name__)
@app.route('/data')
def query_example():
try:
date1 = request.args['date1']
date2 = request.args['date2']
date1_in_system = datetime.strptime( date1, "%Y-%m-%d|%H:%M:%S")
date2_in_system = datetime.strptime( date2, "%Y-%m-%d|%H:%M:%S")
diff = date2_in_system - date1_in_system
dif_in_second = diff.total_seconds()
dif_in_minute = dif_in_second / 60
date1_isdayinweek = datetime.weekday(date1_in_system)
date2_isdayinweek = datetime.weekday(date2_in_system)
fromdate = date(date1)
todate = date(date2)
daygenerator = (fromdate + timedelta(x + 1) for x in range((todate - fromdate).days))
weekend_days_count = sum(1 for day in daygenerator if day.weekday() < 5)
day_left = daygenerator - weekend_days_count
status = 0
limit_days = 2
if day_left > limit_days:
status = "nope, is over 2 days "
if day_left <= limit_days:
status = "can ask for day off "
except Exception as e:
return '''
<h1>get wrong input: {}</h1>
'''.format(date1+ str(e))
return '''
<h1>The date1_in_system is: {}</h1>
<h1>The date2_in_system is: {}</h1>
<h1>The diff is: {}</h1>
<h1>The dif_in_second is: {}</h1>
<h1>The dif_in_minute is: {}</h1>
<h1>The day off status is: {}</h1>
<h1>The date1_isdayinweek: {}</h1>
<h1>The date2_isdayinweek: {}</h1>
<h1>= = = = = = = = = = = = = = =</h1>
<h1>The weekend_days_count is: {}</h1>
'''.format(date1_in_system,date2_in_system, diff, dif_in_second, dif_in_minute,status , date1_isdayinweek, date2_isdayinweek, weekend_days_count)
if __name__ == '__main__':
# run app in debug mode on port 5000
app.run(debug=True, port=5000)
the website output:
(pic) website output
the code picture:
(pic) 1