Summary
In ArcGIS Pro, Python expressions can be used to create a new date field with milliseconds from a specified text data type field with date and time values. This article provides an example and instructions to create a new date data type field by referencing the text field with date and time values using Python expressions in ArcGIS Pro.
The image below shows the attribute table with the text data type date and time field.
Procedure
- In the Contents pane, right-click the feature class and click Attribute Table to open the attribute table of the feature class.
- Click Calculate to open the Calculate Field tool.
Note:
Refer to ArcGIS Pro: Calculate Field (Data Management) for more information on the parameters in the Calculate Field tool.
- In the Calculate Field dialog box, configure the following parameters:
- For Input Table, select the feature class.
- For Field Name (Existing or New), select the field to be edited or create a new field. In this example, the CreationDate field is created.
- For Field Type, select Date.
- For Expression Type, select Python 3.
- Specify the following expression in the first code block. In this example, <textField> is named CreationDateText.
Note:
The date format and separators must be replicated in accordance to the referenced datetime text field. For example, if the datetime text field is displayed as '2023/05/11', specify the datetime Python 3 script as "%Y/%m/%d %H:%M:%S.%f".
datetime.datetime.strptime(!<textField>!, "%d-%m-%Y %H:%M:%S.%f")
-
- Click Verify to run a test of the expression. If the expression is valid, click OK.
- In the Contents pane, right-click the feature class, and click Data Design > Fields to open the fields view of the feature class.
- Click the cell in the Number Format column for the row of the target field to format.
- In the highlighted cell, click the Options button to open the Number Format dialog box.
- In the Number Format dialog box, click the Format drop-down menu and select Custom.
- In the Custom date format text box, edit the date and time format specifiers as required. In this example, dd.MM.yyyy HH:mm:ss.s is used. Refer to ArcGIS Pro: Date and time formats for more date and time format specifiers.
- Click OK and Save on the ArcGIS Pro ribbon in the Changes group.
The image below shows the new date field with milliseconds added to the attribute table.