Created
June 6, 2018 12:26
-
-
Save Fhernd/726fc340c83b7381595156fbb7e9c050 to your computer and use it in GitHub Desktop.
Cálculo del último día. OrtizOL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, timedelta | |
dias = ['Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado', 'Domingo'] | |
def obtener_fecha_ultimo_dia(dia, fecha_inicio=None): | |
if fecha_inicio is None: | |
fecha_inicio = datetime.today() | |
numero_dia = fecha_inicio.weekday() | |
numero_dia_calculo = dias.index(dia) | |
dias_pasados = (7 + numero_dia - numero_dia_calculo) % 7 | |
if dias_pasados == 0: | |
dias_pasados = 7 | |
fecha_calculo = fecha_inicio - timedelta(days=dias_pasados) | |
return fecha_calculo | |
print(datetime.today()) | |
print(obtener_fecha_ultimo_dia('Domingo')) | |
print(obtener_fecha_ultimo_dia('Miércoles')) | |
print(obtener_fecha_ultimo_dia('Viernes')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment