ThinkGeo.com    |     Documentation    |     Premium Support

DateFormat field in TextStyle

This field is supposed to allow you to set the format for data that can be parsed as DateTime data. The documentation is quite sparse (non-existent). Could someone share with me how to use it?

I have a field that is a DataTime, and when I use it to label the object I want to just display the date (preferably in the current system short date format) what should I use?

myTextStyle.DateFormat = ???;

I have tried the only piece of information I could find or “{0:d}” but that does nothing. I still get the full date and time all the way down to seconds. Thank in advance

Hi Chris,

Here’s a bit more detail on how DateFormat is used:

  1. If your text value already contains a format specifier in the form {Text:Format} (for example: {20140506:yyyy/MM/dd} or {MyDateColumn:yyyy/MM/dd}), that inline format will be applied and DateFormat will not be used.

  2. If textStyle.DateFormat is not empty, the map will try to parse the label text as a DateTime like this:

    DateTime.TryParse(text, out dateTime);
    labelText = string.Format(CultureInfo.CurrentCulture, dateFormat, dateTime);
    

    So the value you assign should be a standard string.Format pattern, e.g.:

    myTextStyle.DateFormat = "{0:d}";   // not very sure why it didn't work on your side
    // or
    myTextStyle.DateFormat = "{0:yyyy-MM-dd}";
    
  3. The TextStyle.Formatting event is raised just before FormatCore, and TextStyle.Formatted is raised right after it. You can inspect or modify the text through the event args in those handlers.

  4. Both behaviors in (1) and (2) are implemented in TextStyle.FormatCore(). You can override FormatCore in a derived class and put in your custom logic.

I hope that helps

Thanks,
Ben