Create a time field conversion wizard to handle any time format conversion

Idea created by BHoak-esristaff Employee on May 14, 2015
    New
    Score90
    • Hornbydd
    • incoggis
    • BruceLang
    • trippcor
    • axidentprone3
    • peteschneider
    • sacdou
    • rchasan
    • patrick.taurman
    There are too many date time possible formats to anticipate with tools like the current time convert GP tool, which allow you to pick from one of many possible formats. Although it has many time formats, it didn't support the format we had.

    We need an easy to wizard in desktop, that allows you to construct any time format to correctly identify your current time format. It would allow you to drag and drop in tokens like MM or / or YYYY and assemble the format you currently have (if the software couldn't auto recognize it). Then you could pick one or more output time formatted fields to create. Perhaps this wizard is something useful to add to ArcGIS Pro to enhance the temporal support to any time formatted field possible.

    As an example, this short python script, which solved the problem, can handle any time input and output format, if it the two key lines below were edited slightly. It shows the format that was not supported as an example. I suspect there are others with different unsupported formats that could make use of this script in the meantime.

    import arcpy, os

    from datetime import datetime

    # Input variables
    #fc = "C:/data/timedata.gdb/timedatafc"
    pFeatureClass = arcpy.GetParameterAsText(0) # featureclass

    #parameters for field names
    pStartTimeField = arcpy.GetParameterAsText(1)
    pStartTimeFieldNew = arcpy.GetParameterAsText(2)

    rows = arcpy.UpdateCursor(pFeatureClass)
    print("start")

    for row in rows:
       
        #print(row.getValue("StartTime"))
        StartDateObject = datetime.strptime(row.getValue(pStartTimeField), "%m/%d/%Y %I:%M:%S %p") # ie M/DD/YYYY HH:MM:SS AM
        
        newStartDateStr = StartDateObject.strftime("%Y/%m/%d %I:%M:%S %p") # ie YYYY/MM/DD HH:MM:SS AM
        row.setValue(pStartTimeFieldNew, newStartDateStr)
        #print(newdateStr)
                                                           
        rows.updateRow(row)
        
    # Delete cursor and row objects to remove locks on the data
    del row
    del rows

    print("done")