Created
November 24, 2025 10:37
-
-
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
This file contains hidden or 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
| 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