0

My feature attribute table has a folderpath that indludes a date time. Example:/documents/foldername/xyz2015OCT15063333.shp

It seems very simple, but I am not a programmer and have done my best to calculate the field from examples I have been able to find.

I am using ArcGIS Desktop 10.6.1.

I need this date and time pulled out and placed into a new Date field to enable time.

  • 1
    Welcome to GIS SE. As a new user, please take the Tour. I suggest you uEdit the question to specify the software you are using and give at least one of the attempts you have made, with a description of the problem encountered. – Vince Jul 24 at 14:47
  • 2
    What would be the expected date for you example? Just 15th October of 2015? – Marcelo Villa Jul 24 at 15:55
  • 2
    Furthermore, it is not clear to me if the folderpath is inside the attrbiute table and you are trying to extract the date for each feature using field calculator or if you are creating a new field where all the features have the same date (pulled from the single folderpath) calculated in a new field. – Marcelo Villa Jul 24 at 16:01
1

I dont think date and time in the same field (of date data type) is supported in shapefiles. You can add two fields, one for date and one for time. Example how to get date, use datetime.datetime.strptime:

Return a datetime corresponding to date_string, parsed according to format

import arcpy, re
from datetime import datetime

#import locale
#locale.setlocale(locale.LC_ALL, 'en_US') #Otherwise 'OCT' was not recognized since my locale is SWE = 'OKT'

fc = r"X:\blabla2016OCT151234.shp" #Change
datefield = 'Date123' #Change

ix_first_digit = re.search("\d", fc).start() #Find first digit (so path cant contain digits or you need to change the code)
datestring = fc[ix_first_digit:ix_first_digit+9] #Slice from first occurence of digit and then nine more chars
date = datetime.strptime(datestring, '%Y%b%d') #See: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

#Calculate field using da.UpdateCursor
with arcpy.da.UpdateCursor(fc, datefield) as cursor:
    for row in cursor:
        row[0]=date
        cursor.updateRow(row)
  • This may be helpful for another aspect. Apologies, he ".shp" in my original question was a mistake. That is not a part of the attribute field. The solution I found was Date=datetime.datetime.strptime(!FolderPath!, "%Y%b%m%H%M%S") – rbickett31 Jul 24 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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