Python - Imprimir las Fechas
#Ejemplos de Imprimir las fechas en Python:
#1. Imprimir las fechas de los últimos 10 días:
import datetime
today = datetime.date.today()
for i in range(10):
date = today - datetime.timedelta(days=i)
print(date)
#2. Imprimir las fechas de los próximos 10 días:
import datetime
today = datetime.date.today()
for i in range(10):
date = today + datetime.timedelta(days=i)
print(date)
#3. Imprimir las fechas de un mes específico:
import datetime
year = 2023
month = 7
for day in range(1, 32):
try:
date = datetime.date(year, month, day)
print(date)
except ValueError:
break
#4. Imprimir las fechas de un rango de años:
import datetime
start_year = 2020
end_year = 2025
for year in range(start_year, end_year + 1):
for month in range(1, 13):
for day in range(1, 32):
try:
date = datetime.date(year, month, day)
print(date)
except ValueError:
break
#5. Imprimir las horas de un día específico:
import datetime
year = 2023
month = 7
day = 15
for hour in range(24):
time = datetime.time(hour)
date_time = datetime.datetime(year, month, day, time.hour)
print(date_time)
#6. Imprimir las fechas y horas de un rango de minutos:
import datetime
start_time = datetime.time(8, 0)
end_time = datetime.time(9, 0)
for minute in range(0, 60):
time = datetime.time(start_time.hour, minute)
date_time = datetime.datetime.combine(datetime.date.today(), time)
if start_time <= time <= end_time:
print(date_time)
#7. Imprimir las fechas y horas de un rango de segundos:
import datetime
start_time = datetime.time(8, 0, 0)
end_time = datetime.time(8, 0, 10)
for second in range(0, 60):
time = datetime.time(start_time.hour, start_time.minute, second)
date_time = datetime.datetime.combine(datetime.date.today(), time)
if start_time <= time <= end_time:
print(date_time)
#8. Imprimir las fechas y horas de un rango de microsegundos:
import datetime
start_time = datetime.time(8, 0, 0, 0)
end_time = datetime.time(8, 0, 0, 100000)
for microsecond in range(0, 1000000, 100000):
time = datetime.time(start_time.hour, start_time.minute, start_time.second, microsecond)
date_time = datetime.datetime.combine(datetime.date.today(), time)
if start_time <= time <= end_time:
print(date_time)
#9. Imprimir las fechas y horas de un rango de milisegundos:
import datetime
start_time = datetime.time(8, 0, 0, 0)
end_time = datetime.time(8, 0, 0, 1000)
for millisecond in range(0, 1000, 100):
time = datetime.time(start_time.hour, start_time.minute, start_time.second, millisecond)
date_time = datetime.datetime.combine(datetime.date.today(), time)
if start_time <= time <= end_time:
print(date_time)
#10. Imprimir las fechas y horas de un rango de nanosegundos:
import datetime
start_time = datetime.time(8, 0, 0, 0)
end_time = datetime.time(8, 0, 0, 100)
for nanosecond in range(0, 1000, 100):
time = datetime.time(start_time.hour, start_time.minute, start_time.second, nanosecond)
date_time = datetime.datetime.combine(datetime.date.today(), time)
if start_time <= time <= end_time:
print(date_time)
#11. Imprimir las fechas y horas de un rango de años, meses y días:
import datetime
start_date = datetime.date(2023, 1, 1)
end_date = datetime.date(2023, 12, 31)
current_date = start_date
while current_date <= end_date:
print(current_date)
current_date += datetime.timedelta(days=1)
#12. Imprimir las fechas y horas de un rango de horas:
import datetime
start_datetime = datetime.datetime(2023, 1, 1, 0, 0)
end_datetime = datetime.datetime(2023, 1, 1, 23, 59)
current_datetime = start_datetime
while current_datetime <= end_datetime:
print(current_datetime)
current_datetime += datetime.timedelta(minutes=30)
#13. Imprimir las fechas y horas de un rango de años, meses y días:
import datetime
start_date = datetime.date(2023, 1, 1)
end_date = datetime.date(2023, 12, 31)
for single_date in range((end_date - start_date).days + 1):
current_date = start_date + datetime.timedelta(days=single_date)
print(current_date)
#14. Imprimir las fechas y horas de un rango de horas:
import datetime
start_datetime = datetime.datetime(2023, 1, 1, 0, 0)
end_datetime = datetime.datetime(2023, 1, 1, 23, 59)
for single_datetime in range(int((end_datetime - start_datetime).total_seconds() // 1800) + 1):
current_datetime = start_datetime + datetime.timedelta(minutes=single_datetime * 30)
print(current_datetime)