Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep the date in Airflow execution date and convert time to 00:00

I'm using time_marker = {{execution_date.in_timezone('Europe/Amsterdam')}} in my dag.py program. I'm trying to keep the date part in execution date, and set the time to "T00:00:00" So whenever during the execution date it runs, the time_marker will always be for example 20240115T00:00:00

How should I do this? I tried to use pendulum.parse but didn't work out how to do this. Thanks.

like image 751
user3735871 Avatar asked Dec 23 '25 02:12

user3735871


1 Answers

Add a custom macro to the DAG definition

Function:

def format_execution_date(execution_date):
    amsterdam_time = execution_date.in_timezone('Europe/Amsterdam')
    midnight_amsterdam_time = amsterdam_time.start_of('day')
    return midnight_amsterdam_time.format('YYYYMMDDT00:00:00')

Use function:

with DAG(
...

    user_defined_macros={'format_execution_date': format_execution_date},

) as dag:
...

In your task, you can used by:

params={
    'time_marker': '{{ format_execution_date(execution_date) }}'
}
like image 82
J W Avatar answered Dec 24 '25 21:12

J W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!