Home > Back-end >  (Solved) (Python) date subtract except weekend within parse function not working
(Solved) (Python) date subtract except weekend within parse function not working

Time:09-28

Python date subtract except weekend within parse function not working

I saw other question solution for counting is fixed the input
Loop through dates except for weekends

But I want to stepforward to make various date input possible, thanks

import numpy as np
import re
import  datetime
from datetime import date,  datetime, time ,timedelta
from dateutil import parser
def date_array_operation(input_string1,input_string2,op):
 
    param1_date_only = input_string1.split(" ")
    param2_date_only = input_string2.split(" ")
 
    date1_parse = parser.parse(param1_date_only[0])
    date2_parse = parser.parse(param2_date_only[0])
 
    param1_date_only_new = param1_date_only[0].replace('/','-')
    param2_date_only_new = param2_date_only[0].replace('/','-')
 
    is_d2_bigger = date2_parse > date1_parse


    start = date1_parse
    end = date2_parse
    delta = timedelta(days=1)
    d = start
    diff = 0
    weekend = set([5, 6])
    while d <= end:
        if d.weekday() not in weekend:
            diff  = 1
        d  = delta
 
 
    return is_d2_bigger, diff ;
 
param1 = '2017/09/03 07:11:00'
param2 = '2017-09-05 07:11:00'
param_op = 'hour'
param_result = date_array_operation(param1,param2,param_op)
 
print(param_result)
 

output: (True, 2)

but expect output should be (True, 1)

CodePudding user response:

why the diff is 2? it is because the range your date are 3:

param1 = '2017/09/03 07:11:00'

param2 = '2017-09-05 07:11:00'

and the weekday result is : 6, 0, 1

and in your logic if the weekday is not in set([5, 6]).

it's mean that you in to the condition 2 times.

that is why the diff is 2

CodePudding user response:

so should be weekend = set([0, 6]) not weekend = set([5, 6])
Python DateTime weekday()

0 Monday
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday

import numpy as np
import re
import  datetime
from datetime import date,  datetime, time ,timedelta
from dateutil import parser
def date_array_operation(input_string1,input_string2,op):
 
    param1_date_only = input_string1.split(" ")
    param2_date_only = input_string2.split(" ")
 
    date1_parse = parser.parse(param1_date_only[0])
    date2_parse = parser.parse(param2_date_only[0])
 
    param1_date_only_new = param1_date_only[0].replace('/','-')
    param2_date_only_new = param2_date_only[0].replace('/','-')
 
    is_d2_bigger = date2_parse > date1_parse


    start = date1_parse
    end = date2_parse
    delta = timedelta(days=1)
    d = start
    diff = 0
    weekend = set([0, 6])
    while d <= end:
        if d.weekday() not in weekend:
            diff  = 1
        d  = delta
 
 
    return is_d2_bigger, diff ;
 
param1 = '2017/09/17 07:11:00'
param2 = '2017-09-11 07:11:00'
param_op = 'hour'
param_result = date_array_operation(param1,param2,param_op)
 
print(param_result)
 

  • Related