kpzimmer

DateTime from Feature Class Field not working

Discussion created by kpzimmer on Dec 8, 2011
Latest reply on Dec 9, 2011 by mdenil
I have the following code I have used to get the Date from a field in an shapefile (1/11/2008). Works great but now I have featureclasess that have time (1/11/2008 2:00:59 AM) included in the Date Field so now it doesn't work. I think it has to do with the lstDate split line as its not strDate doesn't have the / between the year and time to split it. Is there a way to either simply only get the date from the date field or remove the time so only the dates are left? Furthermore  every shapefile/featureclass that is used may potentially have a different date field format (e.g. YYYY/MM/DD, or DD/YYYY/MM, or MM/DD/YYYY) so would this be better written to accept date fields in any format. Any suggeustions? Perhaps

    while objFeature:
        # Calculate Image Date
        strDate = objFeature.GetValue(DateField)
        gp.addmessage(strDate)

        lstDate = map(int, strDate.split("/"))
        gp.addmessage("check")
        
        objDate = datetime.date(lstDate[2], lstDate[0], lstDate[1])
        intDay = int(objDate.strftime("%j"))
        intInterval = 0
        for i in range(1, 366, DatesInterval):
            if intDay >= i and intDay < i+DatesInterval:
                intInterval = i
                break
        objFeature.SetValue(FieldName1, str(intInterval))

        # Calculate Raster Path
        objImage = "00" + str(intInterval)
        Year = objDate.strftime("%Y")
Thanks in advance.

Outcomes