1

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

(pic) 2

(pic) 3

davidism
  • 121,510
  • 29
  • 395
  • 339

1 Answers1

1

I cannot understand what result you'd expect. But, try something like this to get all the results without errors.

@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 = datetime.date(date1_in_system)
        todate = datetime.date(date2_in_system)
        daygenerator = [fromdate + timedelta(x + 1) for x in range((todate - fromdate).days)]
        weekend_days_count = sum(day.weekday() for day in daygenerator if day.weekday() < 5)
        print(weekend_days_count)
        print(daygenerator)   

        day_left = len(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 f'''          
              <h1>The date1_in_system is: {date1_in_system}</h1>
              <h1>The date2_in_system is: {date2_in_system}</h1>
              <h1>The diff is: {diff}</h1>
              <h1>The dif_in_second is: {dif_in_second}</h1>
              <h1>The dif_in_minute is: {dif_in_minute}</h1>
              <h1>The day off status is: {status}</h1>
              <h1>The date1_isdayinweek: {date1_isdayinweek}</h1>
              <h1>The date2_isdayinweek: {date2_isdayinweek}</h1>
              <h1>= = = = = = = = = = = = = = =</h1>
              <h1>The weekend_days_count is: {weekend_days_count}</h1>
              '''
Coderio
  • 429
  • 2
  • 9
  • 2
    I'm try to calculate the day off days between date1 and date2, and except weekends. For example `http://127.0.0.1:5000/data?date1=2022-09-30|00:00:00&date2=2022-10-03|00:00:00 ` the expect output is "can ask for day off " cause limit == 2 –  Sep 26 '22 at 10:03