pythondatetimetimedelta

Adjust code that uses timestamp and timedelta: last timestamp of each day must contain a custom timestamp


I'm currently using the code below:

from datetime import datetime, timedelta

initial_date = datetime(1996, 11, 21, 0, 0, 0) 
final_date = datetime(2024, 4, 3, 23, 59, 24)   

time_interval = timedelta(minutes=15)

datetimes_list = []

current_date = initial_date
while current_date <= final_date:
    datetimes_list.append(current_date)
    current_date += time_interval

# print(datetimes_list)

I want to adjust it so that at the end of each day (the code currently consider as final timestamp of each day the 23:45's time) to add a new timestamp at the time 23:59:24. How do I do it? I tried to implement it but I have no idea how to do it.


Solution

  • I ended up getting the desired result:

    from datetime import datetime, timedelta
    
    initial_date = datetime(1996, 11, 21, 0, 0, 0)
    final_date = datetime(2024, 4, 3, 23, 45, 0)  
    
    time_interval = timedelta(minutes=15)
    
    datetimes_list = []
    
    current_date = initial_date
    while current_date <= final_date:
        datetimes_list.append(current_date)
        if current_date.time() == datetime(1, 1, 1, 23, 45).time():
            datetimes_list.append(current_date.replace(minute=59, second=24))
        current_date += time_interval
    
    # print(datetimes_list)