09-12-2024 07:15 AM
I have two inputs of time stamp control "from" which is a starting date, and "to" which is an ending date I want to list out the dates which will lie between them. Can anyone give me some snippet or algorithm through which we can list out the dates?
09-12-2024 08:05 AM
Very brutal solution, but it works. If this is what you want.
I'm sure there are some edge cases that you should check (leap seconds and leap days maybe?).
09-12-2024 08:05 AM
Hi sesinfo,
@sesinfo wrote:
I have two inputs of time stamp control "from" which is a starting date, and "to" which is an ending date I want to list out the dates which will lie between them. Can anyone give me some snippet or algorithm through which we can list out the dates?
What have you tried and where are you stuck?
09-12-2024 08:07 AM
Start with the first date and continuously add 86400 (seconds/day) to get each day until you are past the last date.
09-12-2024 09:57 PM
@Basjong53 wrote:
Very brutal solution, but it works. If this is what you want.
I'm sure there are some edge cases that you should check (leap seconds and leap days maybe?).
That won't always work due to Daylight Saving switchovers.
09-13-2024 08:50 AM - edited 09-13-2024 08:53 AM
@paul_a_cardinale wrote:
@Basjong53 wrote:
Very brutal solution, but it works. If this is what you want.
I'm sure there are some edge cases that you should check (leap seconds and leap days maybe?).
That won't always work due to Daylight Saving switchovers.
That's why I just tell ChatGPT to do it for me in python, then call a python node 😂
Aint no body got time to think about that
from datetime import datetime, timedelta
import pytz
def list_dates_between(start_date, end_date, timezone_str='UTC'):
# Set the timezone using pytz
timezone = pytz.timezone(timezone_str)
# Ensure the start and end dates are timezone aware
start_date = timezone.localize(start_date)
end_date = timezone.localize(end_date)
# List to store the dates between the two timestamps
date_list = []
# Loop through each day and append to the list
current_date = start_date
while current_date <= end_date:
date_list.append(current_date)
current_date += timedelta(days=1)
# Return the list of dates
return date_list
# Example usage:
start_date = datetime(2024, 3, 10, 0, 0) # Day before daylight savings in US
end_date = datetime(2024, 3, 15, 0, 0) # Day after daylight savings starts
timezone_str = 'US/Eastern'
dates = list_dates_between(start_date, end_date, timezone_str)
for date in dates:
print(date.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
09-13-2024 02:33 PM
@UnholyPenguin wrote:
@paul_a_cardinale wrote:
@Basjong53 wrote:
Very brutal solution, but it works. If this is what you want.
I'm sure there are some edge cases that you should check (leap seconds and leap days maybe?).
That won't always work due to Daylight Saving switchovers.
That's why I just tell ChatGPT to do it for me in python, then call a python node 😂
Aint no body got time to think about thatfrom datetime import datetime, timedelta
import pytzdef list_dates_between(start_date, end_date, timezone_str='UTC'):
# Set the timezone using pytz
timezone = pytz.timezone(timezone_str)
# Ensure the start and end dates are timezone aware
start_date = timezone.localize(start_date)
end_date = timezone.localize(end_date)
# List to store the dates between the two timestamps
date_list = []
# Loop through each day and append to the list
current_date = start_date
while current_date <= end_date:
date_list.append(current_date)
current_date += timedelta(days=1)
# Return the list of dates
return date_list# Example usage:
start_date = datetime(2024, 3, 10, 0, 0) # Day before daylight savings in US
end_date = datetime(2024, 3, 15, 0, 0) # Day after daylight savings starts
timezone_str = 'US/Eastern'dates = list_dates_between(start_date, end_date, timezone_str)
for date in dates:
print(date.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
I don't trust deranged bots. Here's how I would do it:
09-15-2024 01:53 AM - edited 09-15-2024 01:56 AM
Just for fun, here are a couple of other ways. I haven't checked if they handle DST transitions correctly.: