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:
install_uliengineering.sh
pip install --user UliEngineering pytzNow you can use UliEngineering.Utils.Date.yield_hours_on_day():
yield_hours_example.py
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 hereOr, if you want to have a list of datetime instances instead of a generator:
yield_hours_list_example.txt
from UliEngineering.Utils.Date import *
hours = list(yield_hours_on_day(year=2022, month=6, day=15, tz=pytz.timezone("Europe/Berlin")))Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow