Dynamic date for yesterday

5639
15
Jump to solution
12-09-2015 10:10 PM
MarkPratt
New Contributor

I am producing some maps and I want the date on the map to be yesterdays date, as the data on the map relates to yesterdays progress.

I can only find in the dynamic date help section how to show this with today's date, when I want today-1.

Any ideas?

Mark Pratt

Western Geco

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
WesMiller
Regular Contributor III

Create a text element in your layout with the "Element Name" = DateTimeElm

Then open python inside arcpmap by clicking on the python tool on your standard tools tool bar. Copy and paste the code below into the python interpreter and click enter twice.

code

from datetime import date, timedelta 
mxd = arcpy.mapping.MapDocument("CURRENT")
#Where my code says "DateTimeElm" your text element needs to be named the same or change this name to reflect your text element
elm = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "DateTimeElm")[0]
elm.text = date.strftime(date.today() - timedelta(days=1), '%m/%d/%Y')
arcpy.RefreshActiveView()

View solution in original post

15 Replies
DanPatterson_Retired
MVP Emeritus

Short of editing the date prior to printing, there is no option as you have discovered, so editing prior to printing or using a text element  and editing it, seem to be the only option

JonMorris2
Occasional Contributor II

Getting yesterday's date is fairly straightforward using python - the datetime module has everything you need.

For example:

from datetime import date, timedelta                       

date.strftime(date.today() - timedelta(days=1), '%d/%m/%Y')

The question is, can you set dynamic text using Arcpy? I'm not sure, but example 2 here seems to sugggest that you can.

Example url: http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/textelement-class.htm

MarkPratt
New Contributor

John

The link to your example doesn't seem to work for me.

It gives me an unexpected error!

Mark

0 Kudos
JonMorris2
Occasional Contributor II

That's weird - Geonet seems to be doing something bad to the link. I tried editing my post but it still didn't work. Try copying the text and pasting it into a new tab.

0 Kudos
DanPatterson_Retired
MVP Emeritus

It is a link to dynamic text, which is what I was suggesting.  If you go into advanced editor, then select the html from the edit line, you can copy and paste the link into your browser.

0 Kudos
MarkPratt
New Contributor

Jon

I have got to the link eventually, but I'm not familiar with Arcpy.

So how would I implement this?

Regards

Mark

0 Kudos
WesMiller
Regular Contributor III

Create a text element in your layout with the "Element Name" = DateTimeElm

Then open python inside arcpmap by clicking on the python tool on your standard tools tool bar. Copy and paste the code below into the python interpreter and click enter twice.

code

from datetime import date, timedelta 
mxd = arcpy.mapping.MapDocument("CURRENT")
#Where my code says "DateTimeElm" your text element needs to be named the same or change this name to reflect your text element
elm = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "DateTimeElm")[0]
elm.text = date.strftime(date.today() - timedelta(days=1), '%m/%d/%Y')
arcpy.RefreshActiveView()

MarkPratt
New Contributor

Wes,

This is great, but is this really any quicker than changing the date in the text element manually each time?

Is there a way of applying this code automatically whenever I open my maps, or can a script be written to update the date on all the maps (5) that I produce on a daily basis, in one operation before opening them?

0 Kudos
WesMiller
Regular Contributor III

Save the code as a python script example "somemeaningfulName.py" then follow these instructions Adding a script tool—Help | ArcGIS for Desktop it will only work if you have the correctly named text element in your layout.