Skip to content

Instantly share code, notes, and snippets.

@The-Final-Apex
Created November 24, 2025 10:37
Show Gist options
  • Select an option

  • Save The-Final-Apex/4e5d7eb51dac07781bbc22f690f6c1d8 to your computer and use it in GitHub Desktop.

Select an option

Save The-Final-Apex/4e5d7eb51dac07781bbc22f690f6c1d8 to your computer and use it in GitHub Desktop.
Update ages in a database when date of birth is given every day at midnight
def update_children_ages():
today = date.today()
kids = Kid.query.all()
for kid in kids:
if kid.date_of_birth:
# Calculate age based on DOB
age = today.year - kid.date_of_birth.year - ((today.month, today.day) < (kid.date_of_birth.month, kid.date_of_birth.day))
kid.age = age
db.session.commit()
# Schedule the update task every day at midnight
scheduler.add_job(func=update_children_ages, trigger="interval", days=1, start_date='2025-03-27 00:00:00', timezone='UTC')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment