Convert DateTime field to Date in Label Expression

4099
4
10-30-2014 08:32 AM
mpboyle
Occasional Contributor III

I have a DateTime field (ex: CreatedDate) and would like to only show the date with the label. How would I setup the label expression to do this using python?

Thanks in advance!

Tags (1)
0 Kudos
4 Replies
Marco_AlejandroBonilla
Occasional Contributor
mpboyle
Occasional Contributor III

Hmmm...I tried for way too long to use python.  Switched to VBScript parser and got what I wanted in a few minutes using FormatDateTime([field],2)...like what is shown in the idea above.

Thanks for the response!

0 Kudos
MarkCederholm
Occasional Contributor III

You cannot use VBScript expressions if you are publishing to ArcGIS Server.  I ran into this problem, and the key to understanding it is found in the first sentence of the third paragraph of the "Building label expressions" help topic:

"Field values are automatically cast to text strings."

Nonetheless, Python expressions are awkward for parsing strings because you can't use square brackets .  For example, in the following expression:

[DATEFIELD].split(" ")[0]

the parser will look for a field called "0" and fail.  Instead, you can use the following:

[DATEFIELD].split(" ").pop(0)

If a field is nullable, then you need to be prepared to handle a null value:

def FindLabel ([DATEFIELD]):

    s = [DATEFIELD]

    if s is None:

        return None

    return s.split(" ").pop(0)

Of course, label performance is adversely affected by using a function, which in turn affects the return time on an ArcGIS Server map service.

[BTW, if you look at the examples, the most appropriate ones for parsing strings are in JScript, and that is probably the better route to take.]

WesMiller
Regular Contributor III

This article ArcGIS Desktop explains date fields it may help. I was able to use below to pull the date.

def FindLabel ( [LASTUPDATE] ):
  d = [LASTUPDATE] 
  a = d.split(" ")
  return a[0]