1

How to convert date, which is in text formated field to date formated field?

Also, there is dates which have only year, without days and months and empty fields.

I've tried Convert Time Field. But it gave me null values

enter image description here

0

1 Answer 1

6

You can use strptime but it is probably easier to use pandas.to_datetime to convert the strings to datetimes. It is smarter/more flexible and can handle different string formats. Add a date field, create a function in Field Calculator or use da.UpdateCursor:

import arcpy
import pandas as pd

table = r'C:\folder\Default.gdb\datedata' #Change to match your data
fields = ['somedateasstring','datefield'] #Change to match your data

with arcpy.da.UpdateCursor(table, fields) as cursor:
    for row in cursor:
        row[1] = pd.to_datetime(row[0], errors='coerce') #Rows that cant be converted to date will be set to None with errors='coerce'
        cursor.updateRow(row)

enter image description here

2
  • What about fields, which for example have only year and month ? The code gives error there.
    – Darius
    Oct 22, 2020 at 5:45
  • Just like in my screenshot and table. No it doesnt
    – BERA
    Oct 28, 2020 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.