2

I am trying to populate a field with the current time. Here is what I tried that did not work:

Start editing
Open Attribute Table
Add Field -> Name: time
    Type: Date
Right Click on Field Name (time)
    Open FieldCalculator
        Parser: Python
        Type: Date

Then I basically tried all of these expressions:

time.strftime('%d/%m/%Y')

That gives me the date, not the time

datetime.datetime.now( )

That gave me DATE and TIME. But as soon as I saved the changes, the time disappears.

(If the time would stay there I could do something with the FieldCalculator to populate another column like !time![:x])

datetime.timedelta(seconds=0)

That gave me <Null>.

Am I missing a step or something?

3
  • Try time.strftime('%H:%M') to get hours and minutes? (Python documentation on strftime)
    – Erica
    Aug 13, 2014 at 14:04
  • That worked sort of. In the edit session it gave me the current time, but as soon as I quit the edit session and save the changes, it sets everything to 12AM
    – four-eyes
    Aug 13, 2014 at 14:06
  • @Erica sorry, didnt link you in my comment
    – four-eyes
    Aug 13, 2014 at 14:30

1 Answer 1

2

The date field stores the full date. It is your system (combined with the type of database) that defines how a date will be displayed.

if you want to store a string with the time using your own format, you can use a text field, then something like

str(datetime.datetime.now( )).split()[1]

will work, or, better, directly use the date formatting from strftime. In your case, H for hours and M for minutes, optionnally S for seconds

time.strftime('%H:%M')

or

time.strftime('%H:%M.%S')

Note that an

3
  • thanks. the str(datetime.datetime.now( )).split()[1] works fine. But the time.strftime('%H:%M') does not work. See my comment on my question.
    – four-eyes
    Aug 13, 2014 at 14:09
  • what kind of database did you use ? I could not reproduce your problem in a gdb.
    – radouxju
    Aug 14, 2014 at 5:51
  • I tried to perform the operation on a table in a shapefile. ArcGis for Desktop 10.2.2
    – four-eyes
    Aug 14, 2014 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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