Formating field values

1657
12
05-13-2013 03:26 PM
MichaelMurphy8
New Contributor
I'm trying to create a feature class with a number of fields, and I've got the data to load properly.  However; I'd like to format the Date field so that the ArcMAP (Identify) always displays the date and time when selected in ArcMAP.  Is there a way to specify the formatting for a specific field value?

Here's what I've got thus far... (I'd like for it to always display as YYYYMMDD HH:mm:SS UTC instead of the default of 12:00:00 AM)

        
public static IFeatureClass createFeatureClassWithSR(string fcn, IFeatureWorkspace fw, ISpatialReference sr, string filname) {
            IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();
            IObjectClassDescription objDesc = (IObjectClassDescription)fcDesc;
            IFields fields = objDesc.RequiredFields;
            IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
            int shapeIdx = fields.FindField(fcDesc.ShapeFieldName);
            IField field = fields.get_Field(shapeIdx);

            IField f18 = new FieldClass();
            IFieldEdit f18Edit = (IFieldEdit)f18;
            f18Edit.Name_2 = "TOA";
            f18Edit.Type_2 = esriFieldType.esriFieldTypeDate;
            f18Edit.AliasName_2 = "TOA";
            f18Edit.IsNullable_2 = false;
            f18Edit.Editable_2 = false;
            DateTime d = new DateTime();
            f18Edit.DefaultValue_2 = d;
            fieldsEdit.AddField(f18);


Thanks
0 Kudos
12 Replies
DuncanHornby
MVP Notable Contributor
Michael,

Are you asking you want to actually see the text " YYYYMMDD HH:mm:SS UTC " as a default rather than "12:00:00 AM" or you want to see "12:00:00 AM" in the "YYYYMMDD HH:mm:SS UTC" format?

From your code you create a datetime object but don't actually set any of its properties, have a look at the examples at the end of this page.

Duncan
0 Kudos
MichaelMurphy8
New Contributor
I know how to format the DateTime ToString output.  What I'm trying to accomplish is to get ArcMAP to display the format I choose when the user highlights a Point on the screen and click the "Identify" button to view the field values.  There must be a way to tell ArcMap how to format the field values when the user selects/views an object on the screen.
0 Kudos
LeoDonahue
Occasional Contributor III
I think what Duncan is asking is whether you are trying to convert what record values are in your date field as they are, or are you trying to store them in your date field in the format you want?

12:00 AM is 12:00 AM.  Did you insert that date value into the record as 12:00 AM?  If yes, then that is all you'll get back out. 

If you are trying to convert 12:00 AM to this: 3/19/2013 12:13:27 PM - I don't think you can.  Someone correct me if I'm wrong.

If you entered the date value as: 3/19/2013, you might be able to convert it to the format you want.

ESRI's field calculator calculates Date() as 3/19/2013
ESRI's field calculator calculates Now() as 3/19/2013 12:13:27 PM
0 Kudos
MichaelMurphy8
New Contributor
If you look at the code snippet I provided, I declare the field as a esriFieldTypeDate, then I store an instance of a DateTime class.  Internally, the DateTime stores the Ticks elapsed since the epoch.  I know I can declare the field as a esriFieldTypeString and store the formatted value, but then it wouldn't be searchable.

I guess what I'm asking, is if there is a way to specify the output format for a field value?  Regardless if it's specifying a double's precision or the timestamp format.  I would think that you could specify it when creating the FeatureClass or FeatureLayer because it's a presentation thing.
0 Kudos
LeoDonahue
Occasional Contributor III
If you look at the code snippet I provided, I declare the field as a esriFieldTypeDate,

I saw it. 
So when you do: DateTime d = new DateTime(); that inserts 12:00 AM into your record? 
If yes, then you need to format the date before it goes in.
0 Kudos
MichaelMurphy8
New Contributor
OK.  I think I found some buggy behavior, because now it's sort-of working.  In the code snippet above, I set the default value of the field to a new DateTime() object -- which defaults to the epoch and should have zero ticks.  It displays as 12:00:00 AM.  After I start setting the kind, year, day, seconds on the instance, it displays as 1/1/2013 12:00:00 AM.  This still isn't desirable, I'd like for it to display in 24 hour time format,
0 Kudos
MichaelMurphy8
New Contributor
I saw it. 
So when you do: DateTime d = new DateTime(); that inserts 12:00 AM into your record? 
If yes, then you need to format the date before it goes in.


Can you give me an example?  I'm not used to C#, the language seems to be a little backwards from Java.
0 Kudos
LeoDonahue
Occasional Contributor III
The samples were in Duncan's post.

http://msdn.microsoft.com/en-us/library/system.datetime.today.aspx

using System;

public class Example
{
   public static void Main()
   {
      // Get the current date.
      DateTime thisDay = DateTime.Today;
      // Display the date in the default (general) format.
      Console.WriteLine(thisDay.ToString());
      Console.WriteLine();
      // Display the date in a variety of formats.
      Console.WriteLine(thisDay.ToString("d"));
      Console.WriteLine(thisDay.ToString("D"));
      Console.WriteLine(thisDay.ToString("g"));
   }
}
// The example displays output similar to the following: 
//    5/3/2012 12:00:00 AM 
//     
//    5/3/2012 
//    Thursday, May 03, 2012 
//    5/3/2012 12:00 AM
0 Kudos
MichaelMurphy8
New Contributor
So, that example only works if I were storing the data into a String.  That's not what I want.  I'm trying to store the data as a esriFieldTypeDate object so that it's searchable.

Do I need to specify the CultureInfo when creating the DateTime object or something to force ArcMAP to display the field in the format that I want?
0 Kudos