Python - Exclude weekends between two Dates

前端 未结 4 428
刺人心
刺人心 2021-01-13 11:08

I want to calculate the difference between the two dates but want to exclude the weekends from it . Below is the format of dates :

CreateDate  - 2017-08-29 1         


        
4条回答
  •  攒了一身酷
    2021-01-13 11:35

    With datetime module:

    import datetime
    
    d1 = datetime.datetime.strptime('2017-08-29 10:47:00', '%Y-%m-%d %H:%M:%S')
    d2 = datetime.datetime.strptime('2017-09-23 16:56:00', '%Y-%m-%d %H:%M:%S')
    delta = (d2 - d1).days
    diff_weekdays = delta - (delta // 7) * 2
    
    print(diff_weekdays)    # 19
    

提交回复
热议问题