I am trying to figure out a way to convert a datetime stamp of the last_edited_date (tracking changes) that is stored in UTC to local time.

I am putting together a map service that allows our heavy equipment folks to update road conditions for our campus during a snow storm. When users view the map I want to show the last edited date for each feature so people know how long ago a road was plowed. I want to pull the last_edited_date field and show it in a label. Unfortunately the time is UTC and the labels do not autoconvert to local browser time when viewing the map service. Since our heavy equipment folks are updating the map via a feature service, switching to Database time to the track changes in sde wont work.

I am hoping someone knows of some kind of expression to either straight up convert or at least know of someway to subtract 7 hours from the time (we are in the pacific time zone).


Python and dates? Gotta be datetime!

This worked for me in ArcMap, though I'm not sure how it will be handled on other machines with different date display formats. Should give you a start, I hope.

Label expression treats data as strings (similarly when rounding numeric fields, you need to cast them) so to easily work with dates you need to convert the string to a datetime object, subtract your offset, and then convert back to a string.

def FindLabel ([date]):
  import datetime
  fmt = "%m/%d/%Y %I:%M:%S %p"
  utc = datetime.datetime.strptime([date], fmt)
  offset = datetime.timedelta(hours=7)
  return datetime.datetime.strftime(utc - offset, fmt)

Here it is displaying in ArcMap. It's not perfect as you can see since the output is zero-padded. enter image description here

  • This did the trick, the only hitch was now that we are in daylight savings I had to subtract 8 hours rather than the 7. Luckily since we are using this for snow removal I shouldn't have to worry about switching it back come spring time, although we have gotten snow as late as June before :) Thank you so much for your help!!! – rwnichols Nov 17 '15 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.