Select to view content in your preferred language

Date field

1564
7
Jump to solution
12-03-2013 08:45 AM
TonyAlmeida
Frequent Contributor
I have a feature class with hundreds of features. The date field in a feature class was populated incorrectly;
1/2/2003.
i am looking for a python code that can add the zero's to the field to make them correct like; 01/02/2013.

Any help would be great.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ShaunWalbridge
Esri Regular Contributor
Try something like this:

  
    input_field = "Date_Time"     expression = 'formatDate(!{input_field}!)'.format(input_field=input_field)     code_block = """ import dateutil.parser def formatDate(input_date):     parsed_date = dateutil.parser.parse(input_date)     return parsed_date.strftime("%m/%d/%Y")"""          arcpy.AddField_management(input_featureclass, "formatted_output_date", 'DATE')     arcpy.CalculateField_management(input_featureclass, field_name, expression, "PYTHON_9.3", code_block)


strftime's %m and %d will produce values with leading zeros where needed, and the dateutil parser will work for dates formatted in other ways as well.
   
cheers,
Shaun

View solution in original post

0 Kudos
7 Replies
ShaunWalbridge
Esri Regular Contributor
Try something like this:

  
    input_field = "Date_Time"     expression = 'formatDate(!{input_field}!)'.format(input_field=input_field)     code_block = """ import dateutil.parser def formatDate(input_date):     parsed_date = dateutil.parser.parse(input_date)     return parsed_date.strftime("%m/%d/%Y")"""          arcpy.AddField_management(input_featureclass, "formatted_output_date", 'DATE')     arcpy.CalculateField_management(input_featureclass, field_name, expression, "PYTHON_9.3", code_block)


strftime's %m and %d will produce values with leading zeros where needed, and the dateutil parser will work for dates formatted in other ways as well.
   
cheers,
Shaun
0 Kudos
TonyAlmeida
Frequent Contributor
Thank you swalbridge for the response, the code you provided only seemed to copy the field but did not add the leading zero's.
The field is a Date field data type, i don't know if that makes a difference or not.
Also can this be done in field calculator?
0 Kudos
MattSayler
Frequent Contributor
If the field is a 'date' data type, then it's probably stored correctly, and that's just how ArcGIS is programmed to interpret for display. Dates are a little different than other data types.

You can pull the dates out in the format you want using strftime like Shaun said. I'm not sure if you can change the default way they're displayed in ArcMap. I didn't find a setting doing a quick search of the usual spots anyway.

Here's a link on strftime and the various '%' codes that can be used for the format string:
http://docs.python.org/2.7/library/datetime.html?highlight=datetime#strftime-and-strptime-behavior
0 Kudos
TonyAlmeida
Frequent Contributor
oh ok i was expecting to pysically see the "0"  in the date field.

Is there a ways to do this in Field calculator?
0 Kudos
MattSayler
Frequent Contributor
Nope. I think it would have to be a setting somewhere that lets you adjust how dates are displayed. You can even try manually typing the date in as "01/01/1950" and it will show up as "1/1/1950" after it updates.

And again, it's really just aesthetics. If you want them in a different format for displaying on a map or in a report, you can probably still do that.

If you need to do math on the dates, you can also use python for that. Here's a thread where someone needed to get time deltas in days:
http://forums.arcgis.com/threads/97156-Calculate-date-difference
0 Kudos
TonyAlmeida
Frequent Contributor
msayler thanks for the info and your help.
0 Kudos
ShaunWalbridge
Esri Regular Contributor
If you change the output type to string, you'll see the leading zeros. But as msayler mentioned, if you store things as dates, they will be displayed based on locale and not in the specified format explicitly.
0 Kudos