0

I have two fields, one is ISSUEDDATE and the other is ISSUEDTIME. I would like to concatenate fields so that the ISSUEDDATE is in m/dd/yyyy hh:mm:ss format.

My ISSUEDTIME field is a string and ISSUEDDATE is a date field and the ISSUEDDATE isn't consistent. Some of the values read as m/dd/yyyy 7:00:00 AM while others are m/d/yyyy. ISSUEDTIME isn't consistent either as some are AM/PM and others are 24 hour time.

How can I accomplish this without adding a field?

I have tried to concatenate the two fields, but it fails. Since there are two time formats, the solution below fails also. I can execute this via selection and field calculate but would like for it to be done in one go.

enter image description here

3
  • 1
    I'd suggest posting sample rows with all types of time and date formats to make things easier. Oct 25, 2017 at 19:23
  • This is very easy to do in Excel. Just add them (don't concatenate); date is no of days since some start, time is the partial day(s). If you can't add an ArcMap field to that table, I would suggest using a testing table so you can try formats.
    – danak
    Oct 25, 2017 at 20:31
  • I don't want to do it in excel because I will lose my original objectID Oct 25, 2017 at 21:10

1 Answer 1

3

This can be done in field calculator. Open a field calculator window for your field IssuedDate. Set the parser to Python and check Show Codeblock. In the pre-logic script code box enter:

import re
from datetime import datetime

def timeconvert(timestr):
    if re.search("PM|AM|pm|am", timestr):
        newtime = datetime.strptime(timestr, '%I:%M:%S %p').time()
    else:
        newtime = datetime.strptime(timestr, "%H:%M:%S").time()
    return newtime

In the code box enter: datetime.combine(datetime.strptime(str( !IssuredDate! ).split(" ")[0] , "%m/%d/%Y"), timeconvert( !IssuedTime! ))

That should take care of the inconsistent entries in the IssuedDate field.

2
  • When ISSUEDTIME is in 24 hour format, a ValueError is thrown for the format, how can this be resolved, otherwise I think it works for the AM/PM format. Oct 25, 2017 at 21:16
  • 1
    I added a function that will parse the time in either 12 or 24 hour formats.
    – Jacob F
    Oct 26, 2017 at 15:04

Your Answer

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

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