Home > front end >  How to have the number of 2 weeks between two dates?
How to have the number of 2 weeks between two dates?

Time:05-03

I would like to know an efficient way to have the number of semi-month between dates.

I have this, code, it work, but I'm sure it could be enhanced ?

import math
import datetime

start_date = datetime.date(2021, 3, 15)

end_date = datetime.date(2022, 5, 30)

delta = end_date - start_date
print(delta.days)

delta = str(delta)
delta = delta[:-13]
delta = int(delta)

nb = int(math.ceil(delta/15))
print(nb)

Output :

441
30

Any ideas how to make this code more efficient ? I'm not sure this is th best way to do that.

CodePudding user response:

You can avoid delta entirely:

nb = int(math.ceil((end_date - start_date).days/15))

Note that, for very long ranges, this may be slightly off (since not all months are 30 days); it depends on exactly what you mean by "semi-month".

  • Related