3

I'm trying to parse a datetime field using python (but have never used python for it before). I have a field that contains a valid date, but not a valid time... and other fields that have valid hour and am/pm information. I need to grab the date (mm/dd/yy) from the datetime field and then combine that with the valid hour and am/pm fields into a new string field.

Below is the vbscript way that I would do this (and it works fine, but I need to build it using python):

    (DatePart ( "m", [STARTDATE])) &"/"& 
    (DatePart ("d", [STARTDATE] )) &"/"& 
    (DatePart ("yyyy", [STARTDATE] )) &" until "&  
    (DatePart ( "m", [ENDDATE])) &"/"& 
    (DatePart ("d", [ENDDATE] )) &"/"& 
    (DatePart ("yyyy", [ENDDATE] )) &" (from "&
    [StartHour] &" "&
    [StartAMPM] &" until "& 
    [EndHour] &" "& 
    [EndAMPM]&" )"

Here's what I've written so far using python (but doesn't work):

    def dateFull(oid)
    startDateObj = datetime.datetime.strptime( !STARTDATE! , "%m/%d/%Y")
    endDateObj = datetime.datetime.strptime( !ENDDATE! , "%m/%d/%Y")
    return startDateObj + " to " +  endDateObj + " from " + !StartHour! + " " + !StartAMPM! + " to " + !EndHour! + " " + !EndAMPM!

Can someone please help me figure out what I'm doing wrong here?

2

Set the python parser (don't use codeblock). I assume all input fields (!StartHour!,...) are string or date. if not bracket it with str() function

standalone python code:

startdate =  '3/1/15'
enddate =  '2/15/15'

starthour = "5"
startAMPM = "PM"
endhour = "6"
endAMPM = "AM"

print str.format("{0} to {1} from {2} {3} to {4} {5}",
startdate,
enddate,
starthour,
startAMPM,
endhour,
endAMPM)

Field calculator:

str.format("{0} to {1} from {2} {3} to {4} {5}",
!startdate!,
!enddate!,
!starthour!,
!startAMPM!,
!endhour!,
!endAMPM!)
  • 1
    str.format() is most definitely your friend here. – Paul Oct 8 '15 at 22:17
  • Thanks for the help guys, but it's still not working. The geoprocessing results box throws error 000539 and says "ValuError: time data '3/1/15' does not match format '%m/%d/%Y'. The fields !STARTDATE! and !ENDDATE! are date type fields, but some fields have only the calendar date and no time, while others have both calendar date and time. – Nick O. Oct 9 '15 at 13:35
  • change '%m/%d/%Y' to '%m/%d/%y' (lower case Y). @NickO. check the updated answer for your attention. – Farid Cheraghi Oct 9 '15 at 17:58
  • @Paul, good point – Farid Cheraghi Oct 9 '15 at 17:59
  • You don't need to cast variables to string as .format() automatically does that for you. str.format("{}", 123) == "{}".format(123) is also another way to write it. – Paul Oct 9 '15 at 18:25
2

It sounds like your issue is more with the malformed dates (some have times, some don't, etc...) than anything else.

You should be able to use a simple Python expression in the field calculator like this:

'{0} to {1} from {2} {3} to {4} {5}'.format(!STARTDATE!.split(' ')[0],
                                            !ENDDATE!.split(' ')[0],
                                            !StartHour!, !StartAMPM!,
                                            !EndHour!, !EndAMPM!)

The important part is the .split(' ')[0] bit. That will return just the date part for dates that have a time and won't fail for those that don't.

Another thing to note is that CalculateField passes dates to Python as strings instead of datetime objects for some reason (so no need to cast).

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.