Job parameters to get date and time

mh_db
Databricks Partner

I'm trying to set up a workflow in databricks and I need my job parameter to get the date and time. I see in the documentation there's some options for dynamic values.

I'm trying to use this one: {{job.start_time.[argument]}}

For the "argument" there, is it possible to have multiple arguments? if so, what's the right sintax? I'm trying to used  yearmonthdayhourminutesecond as I see in the documentation these are valid arguments, but seems it's only taking one at a time. How do I get all?

brockb
Databricks Employee
Databricks Employee

Hi,

You have different options, one could be to setup the job with multiple parameters. So one parameter called `year` is represented by `{{job.start_time.[year]}}`, another `month` represented by `{{job.start_time.[month]}}`, etc. 

Alternatively, you could pass a single argument like `{{job.start_time.[iso_datetime]}}` and then parse each of the values of interest in your program. For example:

```
import datetime

iso_datetime: str = dbutils.widgets.get("iso_datetime")
date_object = datetime.datetime.strptime(iso_datetime, "%Y-%m-%dT%H:%M:%S.%f")
print(date_object.year, date_object.month, date_object.day)
2024 5 1
```

 
Hope this helps.

mh_db
Databricks Partner

I defined the job parameter in job using {{job.start_time.[iso_datetime]}} and then I added the lines you suggested in notebook but getting an error "com.databricks.dbutils_v1.InputWidgetNotDefined: No input widget named iso_datetime is defined"

import datetime
iso_datetime: str = dbutils.widgets.get("iso_datetime")
date_object = datetime.datetime.strptime(iso_datetime, "%Y-%m-%dT%H:%M:%S.%f")
print(date_object.year, date_object.month, date_object.day)

brockb
Databricks Employee
Databricks Employee

When adding parameters to the job under "Edit Parameters", you provide a `Key` and then the `Value`. In my example, the `Key` was `iso_datetime` (which is what was referenced in the notebook) and the `Value` was `{{job.start_time.[iso_datetime]}}`.

What was the name of your `Key`?

mh_db
Databricks Partner

I have LoadID as key and value {{job.start_time.[iso_datetime]}}

brockb
Databricks Employee
Databricks Employee

Then please change the code to:
```
iso_datetime = dbutils.widgets.get("LoadID")
```