1

In QGIS 3.2.3: I have a layer/Shapefile with a date field (type DATE) that comes from a joined table. I would like to symbolize the points which are older than 10 days (based on the current date) in another color than points which are younger than 10 days (based on the current date). It would also be possible not to display the younger points.

Is there a way to use the current date to make such visualisations? Maybe with a rule based symbology: I thought in using an expression with the now() function, which returns the current date and time and then remove 10 days from it, but couldn't get it going. Am I missing something?

2

Try using a combination of age() (which calculates the difference between the first date minus the second date, and returns an interval) wrapped in day() (which converts the interval to days as a number).

So for example it is now 8 Nov 2018, 2:18 am. If my date field is 2018-10-25, I could find out the difference in days by doing:

day(age(now(),"date"))

which returns 14.09. In other words date was 14.09 days ago from now.

You may wish to wrap your now() variable in to_date() to make sure you are counting only whole days.

To style your points you would use a rule like day(age(to_date(now()),"date")) > 10 for dates that are more than 10 days before the current date, 0 < day(age(to_date(now()),"date")) =< 10 for dates that are between 0-10 days (inclusive of 10) before the current date.

Note that if you have a negative value for the expression before the operator it means your date is in the future.

edited to add: csk is right, you would need either a virtual field or an auto-updating field (see attribute field properties under Layer > Properties) for this to update whenever now() changes.

  • thank you, that solved my question. I use the following 2 rules: day(age(to_date(now()),"testexcel joined table")) > 10 for the old dates and the rule day(age(to_date(now()),"testexcel joined table")) <= 10 for the new dates. This also works when using the joined field name. – GIS7 Nov 7 '18 at 15:56
  • 1
    Be sure to use a virtual field if you want this to auto-update. A non-virtual field will run the calculation once, and never update. – csk Nov 7 '18 at 17:05
  • whick calculation are you talking about csk? The expression is entered in the rule based symbology dialogue. I guess this is just relevant when using the field calculator, right? – GIS7 Nov 8 '18 at 7:10
  • 1
    What csk means if you want the expression to be dynamic (updates the value of now() every time you open the file and therefore the number of days between your date field and now()) you need to use a virtual field or the "set default value on update" feature in QGIS3.x. And then your rule based symbology would refer to that field. Unless you wanted this calculation to be once off (so you set it up on 08 Nov and if you open the project 12 Nov it won't change any values) – she_weeds Nov 8 '18 at 7:28
  • 1
    Hm, I admit I hadn't thoroughly tested out the behaviour of now() in the rule-based symbology, as what csk mentioned sounded right with my experience of other dynamic expressions. If it has updated correctly then I guess you're good! Thanks for clearing that one up... For what it's worth, what I meant by virtual field is that you create one in field calculator with the exact same expression, and say you name it "virt". You will have values of 1 or 0 (I.e. true or false as your expression is a logical test). Then you would style your data according to "virt". – she_weeds Nov 8 '18 at 9:20

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.