Airflow: Convert one digit to two digit with execution_date
I am trying to convert my airflow date from one digit to two digit. At the moment when I use {{ execution_date.month }}
my month becomes 1
but would like it to become 01
.
I have tried with {{ execution_date.month.strftime("%m") }}
but get the error:
int object' has no attribute 'strftime
Solution 1:
You can do:
from datetime import datetime
from airflow.models import DAG
from airflow.operators.bash import BashOperator
with DAG(
dag_id="bash_example",
schedule_interval=None,
start_date=datetime(2022, 1, 19),
catchup=False
) as dag4:
BashOperator(task_id="task", bash_command="echo {{ execution_date.strftime('%m') }}")
Or
from datetime import datetime
from airflow.decorators import dag
from airflow.operators.bash import BashOperator
@dag(schedule_interval=None, start_date=datetime(2022, 1, 19), catchup=False)
def bash_example():
BashOperator(task_id="task", bash_command="echo {{ execution_date.strftime('%m') }}")
dag = bash_example()
Rendering:
Logs: