how do I change date format in labels

14004
6
09-15-2014 11:42 AM
JamesHarrington
Regular Contributor

Hello,

 

I want to label point features with a date field. The labels show long date and time. I do not want to show the time, just the date. Is there a way to format the label to show only the date?

6 Replies
DarrenWiens2
MVP Honored Contributor

You can change date label format in the label expression.

VBScript:

DateValue([date])

Here are the VBScript Date functions.

Likewise, search for Python date functions.

JasonBalmut
Frequent Contributor

Let's say I take a date field which has values like: 7/23/2015 7:00:00 AM.  When I use the VB function TIMEVALUE to label the field it returns 7:00:00 AM.

If I want it to return 7 AM, how do I format the VB line?

0 Kudos
SepheFox
Deactivated User

You can find a list of time format tags here: ArcGIS Help 10.1. Essentially, you would use the tag, format= "h tt".

0 Kudos
OwenEarley
Frequent Contributor

Hi James,

You can use a python function for the label expression to change your date format. Click on the Expression button on the Label properties then:

  1. Select your date field.
  2. Check the Advanced option
  3. Change the Parser option to Python
  4. Copy and paste the python function (below) into the expression
  5. Change the input datetime format if required (in the example this is '%d/%m/%Y %H:%M:%S %p')
  6. Change the output format if required (in the example this is '%d, %b %Y')
  7. Click Verify to test and OK to apply

date-labels.png

Python function for the label expression:

from datetime import datetime

def FindLabel ( [TestDate] ):

  d = datetime.strptime([TestDate], '%d/%m/%Y %H:%M:%S %p')

  return d.strftime('%d, %b %Y')

A list of format strings is available here:

Hope this helps.

Owen

Spatial XP

JamesHarrington
Regular Contributor

Unfortunately, I have some dates with times, some without. None of these solutions work without writing a much more complex script to identify the format. This is much too complicated. Why can't it recognize what format, and allow us the option to display it in whatever format we choose?

0 Kudos
BrandonLee1
Regular Contributor

I have the same question regarding displaying only the month and day of the date.

This is a snip of the date field in ArcMap

I've tried this code:

from datetime import datetime
def FindLabel ( [StartDate] 😞
   d = datetime.strptime([StartDate], '%m/%d/%Y %I:%M:%S %p')
   return d.strftime('%d %m')

This is the error I am getting when I verify the expression.  I read this so I know that the zero-padded date is not an issue..

Any insight on why the error is showing?

0 Kudos