How to generate datetime for every hour on a given day in Python
This example code generates a timezone-aware datetime for every hour on a given day (minutes & seconds are always set to 0
) in a given timezone.
First, install the UliEngineering library and pytz
for timezones:
pip install --user UliEngineering pytz
Now you can use UliEngineering.Utils.Date.yield_hours_on_day()
:
from UliEngineering.Utils.Date import *
for hour in yield_hours_on_day(year=2022, month=6, day=15, tz=pytz.timezone("Europe/Berlin"):
pass # TODO: Your code goes here
Or, if you want to have a list
of datetime
instances instead of a generator:
from UliEngineering.Utils.Date import *
hours = list(yield_hours_on_day(year=2022, month=6, day=15, tz=pytz.timezone("Europe/Berlin")))