0

I thought this was fairly straightforward; however I am receving NameError: name 'dt' is not defined. What is being done incorrectly? My orginal timestamp is m/d/yyyy h:mm:ss i would like to change it to %m/%d/%Y

 >>> rows = arcpy.UpdateCursor(aTable)
    ... for row in rows:
    ...     datetimeVal = row.getValue("last_edited_date")
    ...     formattedTime = dt.strftime(datetimeVal, "%m/%d/%Y")
    ...     print formattedTime
    ...     row.setValue("PickupDate", formattedTime)
    ...     rows.updateRow(row)
    ... del rows, row

closed as off-topic by PolyGeo Dec 3 '15 at 21:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions relating to general Information Technology, with no clear GIS component, are off-topic here, but can be researched/asked at Stack Overflow (software development), Super User (computing hardware and software) and Database Administrators (relational databases)" – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    did you import datetime with an alias? – fluidmotion Dec 3 '15 at 18:26
  • no, I was under the impression that dt is a method of strftime – Geoffrey West Dec 3 '15 at 18:44
  • This looks more like a Python (Stack Overflow) than ArcPy (Geographic Information Systems) problem. Can you present a code snippet that works up to the error, please? As an aside, unless you are using a version earlier than 10.1 this will be better written using arcpy.da for performance. – PolyGeo Dec 3 '15 at 19:56
  • @GeoffreyWest insert datetime.date or datetime.datetime object directly. format time with datetime.strptime only if you whant print date or set it on a texte field – GeoStoneMarten Dec 3 '15 at 20:49
  • For you information arcpy use date in this format "%Y-%m-%d %H:%M:%S". Then that is formated in table to watch date in local format. ArcGis use locales environnement variables to that – GeoStoneMarten Dec 3 '15 at 20:55
3

Here is a code sample from the documentation. Here, they assign dt to a variable. You need to import datetime and then create a dt variable from your datetime value. Look how they do it in the sample and check out the context from the rest of it for more information.

>>> from datetime import datetime, date, time
>>> # Using datetime.combine()
>>> d = date(2005, 7, 14)
>>> t = time(12, 30)
>>> datetime.combine(d, t)
datetime.datetime(2005, 7, 14, 12, 30)
>>> # Using datetime.now() or datetime.utcnow()
>>> datetime.now()   
datetime.datetime(2007, 12, 6, 16, 29, 43, 79043)   # GMT +1
>>> datetime.utcnow()   
datetime.datetime(2007, 12, 6, 15, 29, 43, 79060)
>>> # Using datetime.strptime()
>>> dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
>>> dt
datetime.datetime(2006, 11, 21, 16, 30)
>>> # Using datetime.timetuple() to get tuple of all attributes
>>> tt = dt.timetuple()
>>> for it in tt:   
...     print it
...
2006    # year
11      # month
21      # day
16      # hour
30      # minute
0       # second
1       # weekday (0 = Monday)
325     # number of days since 1st January
-1      # dst - method tzinfo.dst() returned None
>>> # Date in ISO format
>>> ic = dt.isocalendar()
>>> for it in ic:   
...     print it
...
2006    # ISO year
47      # ISO week
2       # ISO weekday
>>> # Formatting datetime
>>> dt.strftime("%A, %d. %B %Y %I:%M%p")
'Tuesday, 21. November 2006 04:30PM'
>>> 'The {1} is {0:%d}, the {2} is {0:%B}, the {3} is {0:%I:%M%p}.'.format(dt, "day", "month", "time")
'The day is 21, the month is November, the time is 04:30PM.'

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