I want to make a date and time updated field as I edit an attribute field in this format yyyy-mm-dd 00:00:00 (= 2016-05-08 11:04:00) - my local date/time. The data is in a QGIS 2.14 shapefile and vector lines as screenshot below.

When a feature as NULL in the 'name' field, I want to enter a name for the road and the 'mod' field must receive de modified date/time like '2016-05-08 15:16:00'.

I already tried some options and failed. I need help to start from beginning.

  1. I create a new field, date/time type, but stays with NULL value after I edited, in the "name" field, or the feature itself.
  2. I used too, the expression now(), but stays with NULL value after I edited.

Note: the date seen there, 2016-04-14 is the first value created with "Field Calculator".

enter image description here

I am using QGIS 2.14.

  • Please edit your question to specify the data format and the versions of software in use. You should also specify the datatype of the target column (since date fields don't have formats)
    – Vince
    Commented May 7, 2016 at 13:17
  • Please edit the question to contain the requested information. You should detail what you have tried, and exactly how it failed. This will help allow our volunteers to understand what you are trying to achieve. As it stands, there isn't enough information to more than guess at a solution -- I'm imagining an autopopulate column in DB2 isn't going to meet your needs.
    – Vince
    Commented May 7, 2016 at 16:41
  • Sounds to me if you would like to store the timestamp of the last edit/update of a feature in an attribute, did I get you right? Something like what ESRI calls 'editor tracking'? Commented May 7, 2016 at 17:21
  • 2
    so what kind of data provider are you using (shape, postgis, ...) and by the way: is the 3-digit month a typing error or do you need something like 2016-may-08? perhaps catching the ´beforeCommitChanges´ signal of a ´QgsVectorLayer´ or an ´UPDATE´ trigger in postgesql might be successful. Commented May 8, 2016 at 9:19
  • 1
    Shapefile is a miserable data format for timestamps, since dBase only supports day resolution (it doesn't really support NULLs either). You need to use a character column for this information (which would then need to be parsed for any further use)
    – Vince
    Commented May 8, 2016 at 15:10

3 Answers 3


You can use the following code which connects the attributeValueChanged event to a function we can define which inserts the results of the $now expression. Highlight your layer and copy/paste the following into the Python Console:

layer = qgis.utils.iface.activeLayer()

def update():
    field = layer.fieldNameIndex('mod')
    e = QgsExpression( " $now " )
    e.prepare( layer.pendingFields() )
    for feat in layer.selectedFeatures():
        feat[field] = e.evaluate( feat )
        layer.updateFeature( feat )


Select the feature by clicking the row number (shown in the red box in the image) or from the map canvas and edit any attribute for that feature. The attribute in your mod field should update:


Note: I used a string field instead of date in order to get the time, otherwise the date field only records YYYY-MM-DD.

  • @CarlosPires - Most welcome! Hope it helps :)
    – Joseph
    Commented May 10, 2016 at 9:04
  • so I created a new text string(20) field 'mod' (see 1), I pasted your code into the Python console (after ctrl-alt-P), insert the name of the street 'name' (see 2), left the record but the field is still NULL (see 3) as you see in the screen capture. ![ScreenshotTry] (i.sstatic.net/lGR2D.png). Commented May 10, 2016 at 21:35
  • 2
    Now it works! As I am not a programmer did not know we had first run the code !! :( Sorry. Note:It works with the select tool. If the attribute field is edited with the info tool (identify feature), it doesn´t work. Commented May 11, 2016 at 14:32
  • 2
    @CarlosPires You can use the AutoFields plugin for this. Watch this video for an example of automatic calculation of date and time. I guess it should also work if you use the identify tool to update your values, could you please check? Commented Aug 19, 2016 at 16:44
  • 1
    @GermánCarrillo, it seems a good choice. I will try later. Thanks. Commented Aug 21, 2016 at 12:05

I stumbled upon this Q&A while I was looking for a way to date/time stamp changes I was making to records using a plugin. I placed a slightly modified version of the code into the plugin to automatically update the date/time into the record(s) I changed:

""" Prepare Change Date/Time Stamp"""
e = QgsExpression( " $now " )
cDate = e.evaluate()

"""" Identify column(s) to change and new value(s) to assign"""
attrs = { 10 : newStat, 20 : cDate }

""" Make record change and capture date/time of change'''
layer.dataProvider().changeAttributeValues({ fid : attrs })
  • Technically this seems not to be an answer but I think this "answer" may have some value to a future reader as part of the Q&A and so I am leaving it in place. It needs more formatting than converting it to a comment would allow. I'm happy to be overruled if anyone closer to writing QGIS Plug-ins than I am wants to make a case in comments here and then ping me or another moderator.
    – PolyGeo
    Commented Sep 4, 2016 at 21:49

I just selected now() function on default value expression and it worked.. very simple

I am using QGIS 3.14 PI

Just create a field with DateTime format and set default as now().

Right click on layer and select properties --> Attribute Forms-->select date field --> defaults--> Default Value Expression--> type now().

Apply and save.

Create new features and see. Timestamp will be updated into the field.

Your Answer

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

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