convertdates

Convert date characters in a string into Python datetime format.

This guide explains how to configure the convertdates instruction. To learn about another instruction, go back to the Available custom script instructions overview.

Introduction

Use the convertdates instruction to convert dates into Python datetime format. By default, this instruction uses the Python module and class datetime.date. You may need to use this instruction before other instructions that need dates in Python datetime format, such as select.

Converting dates into Python datetime format means that you can then use this datetime column in other custom script instructions, provided that these are performed after the convertdates instruction. The example configuration below explains this in more detail.

Creating a custom script transformation using the convertdates instruction

To create and configure a custom script using the convertdates instruction, follow these steps:

  1. Create a custom script transformation.

  2. In the Configuration step, select the convertdates instruction.

  3. To configure the custom script instruction, fill in the required fields below, as well as any optional fields you require:

Fields (required)

Enter the name of the column that contains the dates to convert. To add more columns, click .

Auto or Format (required)

Choose how to specify the original format of the dates to be converted:

  • Select the Auto checkbox for Adverity to automatically detect the format of the dates.

  • In the Format field, enter the format of the dates to be converted. For example, the format of the date 2020-03-31 is %Y-%m-%d.

Datetime

Select this field to convert the dates to date and time (this uses Python module and class datetime.datetime).

Leave this field unselected to only convert the dates to a date format (this uses Python module and class datetime.date ).

Strict

Select this checkbox to use the value None in the new column if the date conversion fails.

Example

Transformation configuration

In this example, we want to use the select custom script instruction to keep the rows in our data extract where the campaign start date is in January (represented by 01 as the month). Currently, the Campaign start date column has the datatype String. As a result, we can't use the date in the select instruction.

We can first use the convertdates instruction to convert the dates in this column to Python datetime format using the following configuration:

Fields

Campaign Start Date

Format

%d-%m-%Y

Datetime

Leave this field unselected.

Data table before transformation

Campaign

Campaign Start Date
(datatype String)

Brand

10-01-2022

Outreach

15-01-2022

Organic Growth

28-02-2022

Data table after transformation

Campaign

Campaign Start Date

(datatype Day)

Brand

2022-01-10

Outreach

2022-01-15

Organic Growth

2022-02-28