Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am writing an arcpy tool script in ArcGIS 10.0. In a fGDB feature class, I have a Date type field. I gather that the values in the Date field are stored as long integer dates, but they display in mm/dd/yyy format in the table. What I want to do is capture the integer date value as a string showing the date in mm/dd/yyyy format. How do I go about this?

There are many examples of how to convert a date/time string to a date object/value but not vice versa. I am appending features from a feature class with many fields, including a date field, into a standard-schema fc (which I can't change) that has few fields. To save a lot of the technical attributes in the input, I must dump it all into a catch-all, user-controlled text field that is defined in the target. The date data from the input is one of those pieces of data I need to save, hence the need to convert the date to a string.

There is likely a fairly easy solution I'm missing, but at the moment I am at a loss. I appreciate any help that comes my way.

share|improve this question
    
It's been months, have you solved your problem? –  R.K. Feb 7 '12 at 1:38
1  
@R.K. - I'm very sorry to have left those interested hanging on this one. I lost track. Anyway, yes, blah238's answer below worked just fine. The particular script I was working on was subsequently changed in a way that eliminated the need for the date conversion, but I have used this solution multiple times since in other scripts. Thanks, R.K., for asking, and thank you blah238 for you help. –  celticflute Jan 18 '13 at 15:13

1 Answer 1

up vote 15 down vote accepted

GP cursors read date values as datetime objects, so you can use datetime.strftime() to format it as you like, or datetime.ctime() to format it as the default format (%a %b %d %H:%M:%S %Y).

Here's an example using mm/dd/yyyy:

import arcpy
from datetime import datetime
fc = r"C:\GISData\test.gdb\atlantic_hurricanes_2000"
rows = arcpy.SearchCursor(fc)
for row in rows:
    datetimeVal = row.getValue("Date_Time")
    formattedTime = datetime.strftime(datetimeVal, "%m/%d/%Y")
    print formattedTime

For the meanings of the various time formatting codes see strftime behavior.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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