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

How you described it was exactly how I thought it was supposed to work.

In my instance, I have another place where I grab the information from the column, I read the field values and convert them to date / time the same way you do, and in that case it does return the date time as I expect. So why it is not working in the TextStyle I too am at a loss.

I will do some more testing, and see what I can

Found the issue, I was trying to define both the NumericFormat and the DateFormat in the same TextStyle, turns out you are only allowed to define one of the formats and only one. I now have it set to define the one I need for the field I want to use.

All is working now as expected

You’re right — this was a limitation in earlier versions. We’ve fixed it in 14.5.0-beta034 , and the map will now correctly parse both NumericFormat and DateFormat when they’re defined together.

Pull the latest beta and give it a try.