In QGIS >2.18, is it possible to style features based on the age gap between the current date and date recorded in the attribute table. For instance I have purchased a significant amount of OS data for different mapping projects with an annual licence. I have a shapefile which hosts the extent of the purchase area and includes the expiry date for each licence/feature. I would like to apply a graduated style to the shapefile which will give me a visual indication if the licence for a particular area has expired or not. The shapefile can be loaded into QGIS from a spatialite database if it is necessary.

Just wondered if QGIS has the capability to run an expression based off the current date and update the styling every time the layer is loaded into the canvas.


1 Answer 1


Using the Field Calculator, create an integer-type Virtual Field with an expression like this:

minute( age( now() , "licensedate" )) > 0

This field will read 1 (true) if the license date is earlier than the current time. The field will be 0 when the license date is later than now.

  • If your license dates are not precise to the minute, replace the minute() function with hour(), day(), week(), month() or year().
  • Replace the field name "licensedate" with the name of your license date field.

A note about virtual fields:

Virtual fields are saved as part of the QGIS project file. If you want that information as part of the shapefile, you must save it as a static (regular) field. Static fields are not auto-updated, so the information in a static field will only be as current as the last time you updated it.

Warning In QGIS 3.0 and later, there are two bugs with virtual fields. Use them at your own risk, and save the project frequently. Or just use a static field, and update it manually (using the field calculator) as needed.

  1. If you edit a layer with virtual fields, when you try to save the edits QGIS gives an error message like Layer has too many attributes (expecting [x], received [y]). The edits are usually saved without issue, so this bug it mostly annoying. Link
  2. If you try to delete a virtual field (eg, because the first issue gets too annoying), QGIS crashes. Link
  • Great, thank you so much for raising the idea of virtual fields, if only I had known...
    – James B
    Jul 12, 2018 at 9:07
  • Happy to help. If you're using QGIS 3.0 or later, you should be aware of this issue with virtual fields that causes QGIS to crash. Until that issue is resolved, it's probably best to use a static field and update it manually when using QGIS 3.0.
    – csk
    Jul 12, 2018 at 16:03
  • 1
    to prevent using virtual field you can use the same fonction as a rule based styling
    – J.R
    Jul 12, 2018 at 16:21

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.