OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Get the datetime range between two dates

  • Thread starter Thread starter emiley mille
  • Start date Start date
E

emiley mille

Guest
I have implemented below code to get date range -

Code:
from datetime import timedelta, date,datetime

def daterange(date1, date2):
    # Iterate over the range of days between date1 and date2
    for n in range(int((date2 - date1).days) + 1):
        # Yield each date within the range
        yield date1 + timedelta(n)

##Logic to fetch data in daterange
current_date = datetime.today()
# day = int(current_date.strftime('%d'))
day = 19
year = int(current_date.strftime('%Y'))
month = int(current_date.strftime('%m'))

if str(day) == '19':
    # Define the start date
    start_dt = date(year, month, 18)
    # Define the end date
    end_dt = date(year, month, day)
elif str(day) == '28' or str(day) == '30' or str(day) == '31' or str(day) == '29':
    # Define the start date
    start_dt = date(year, month, 16)
    # Define the end date
    end_dt = date(year, month, day)

# Iterate over the range of dates generated by the daterange function
for dt in daterange(start_dt, end_dt):
    # date in the format YYYY-MM-DD
    dated = dt.strftime("%Y-%m-%d")
    startdate = str(dated) + 'T00:00:00Z'
    enddate = str(dated) + 'T03:00:00Z'
    print(startdate)
    print(enddate)

It gives below output -

Code:
2024-06-18
2024-06-18T00:00:00Z
2024-06-18T03:00:00Z
2024-06-19
2024-06-19T00:00:00Z
2024-06-19T03:00:00Z

I want to return this date range in interval of two hours for each day- Expected output -

Code:
2024-06-18T00:00:00Z
2024-06-18T02:00:00Z
2024-06-18T02:00:00Z
2024-06-18T04:00:00Z
2024-06-18T04:00:00Z
2024-06-18T06:00:00Z
2024-06-18T06:00:00Z
2024-06-18T08:00:00Z......
2024-06-18T22:00:00Z
2024-06-19T00:00:00Z......
2024-06-19T00:00:00Z
2024-06-19T02:00:00Z....

Any help would be appreciated.
<p>I have implemented below code to get date range -</p>
<pre><code>from datetime import timedelta, date,datetime

def daterange(date1, date2):
# Iterate over the range of days between date1 and date2
for n in range(int((date2 - date1).days) + 1):
# Yield each date within the range
yield date1 + timedelta(n)

##Logic to fetch data in daterange
current_date = datetime.today()
# day = int(current_date.strftime('%d'))
day = 19
year = int(current_date.strftime('%Y'))
month = int(current_date.strftime('%m'))

if str(day) == '19':
# Define the start date
start_dt = date(year, month, 18)
# Define the end date
end_dt = date(year, month, day)
elif str(day) == '28' or str(day) == '30' or str(day) == '31' or str(day) == '29':
# Define the start date
start_dt = date(year, month, 16)
# Define the end date
end_dt = date(year, month, day)

# Iterate over the range of dates generated by the daterange function
for dt in daterange(start_dt, end_dt):
# date in the format YYYY-MM-DD
dated = dt.strftime("%Y-%m-%d")
startdate = str(dated) + 'T00:00:00Z'
enddate = str(dated) + 'T03:00:00Z'
print(startdate)
print(enddate)
</code></pre>
<p>It gives below output -</p>
<pre><code>2024-06-18
2024-06-18T00:00:00Z
2024-06-18T03:00:00Z
2024-06-19
2024-06-19T00:00:00Z
2024-06-19T03:00:00Z
</code></pre>
<p>I want to return this date range in interval of two hours for each day-
Expected output -</p>
<pre><code>2024-06-18T00:00:00Z
2024-06-18T02:00:00Z
2024-06-18T02:00:00Z
2024-06-18T04:00:00Z
2024-06-18T04:00:00Z
2024-06-18T06:00:00Z
2024-06-18T06:00:00Z
2024-06-18T08:00:00Z......
2024-06-18T22:00:00Z
2024-06-19T00:00:00Z......
2024-06-19T00:00:00Z
2024-06-19T02:00:00Z....
</code></pre>
<p>Any help would be appreciated.</p>
 
Top