Calculate 9:00 PM in between two dates in python
Question: How can I calculate how many times 9:00 PM can come in between two dates?

    
    
    dt_start_time = "2014-09-23 11:00:00"
    dt_end_time   = "2014-09-24 12:00:00"
    

Expected Output is 1

    
    
    dt_start_time = "2014-09-24 09:00:00"
    dt_end_time   = "2014-09-24 22:00:00"
    

Expected Output 1

    
    
    dt_start_time = "2014-09-24 09:00:00"
    dt_end_time   = "2014-09-26 12:00:00"
    

Expected Output 2


Answer: You can do this simply by converting the time difference between the two dates
to days, and add an extra day if the dates range misses an extra 9 PM because
of roundoff

    
    
    dt_start_time = "2014-09-23 11:00:00"
    dt_end_time = "2014-09-24 23:00:00"
    
    format = "%Y-%m-%d %H:%M:%S"
    
    from datetime import datetime as dt
    
    st = dt.strptime(dt_start_time, format)
    end = dt.strptime(dt_end_time, format)
    
    count = (end - st).days
    if st.hour < 21 and end.hour >= 21:
        count += 1
    print count
    

