How to style by date monthly ranges QGIS?

I do not suggest using the date functions, because the distance from e.g. 2019-10-28 to 2019-11-1 would be 0 where you expect 1. Instead access the year and month via substr function and calculate the distance in months say from 10/2019 onwords. The following Expression does the trick:

(to_int(substr("my_date", 0, 4)) - 2019) * 12 + (to_int(substr("my_date", 6, 2)) - 10)

For testing purposes I created a dataset and a virtual field as follows:

enter image description here

result:

enter image description here

Then I use a categorized renderer like this:

enter image description here

resulting in

enter image description here


this expression calculates the difference in months from now on and a date in the future

if(
year(now()) = year("my_date"), 
month("my_date") - month(now()), 
month("my_date") - month(now()) + (12 * (year("my_date") - year(now())))
)

enter image description here

To style you can use a rule-based style using 6 rules:

  • current month: if(year(now()) = year("my_date"), month("my_date") - month(now()), month("my_date") - month(now()) + (12 * (year("my_date") - year(now())))) = 0
  • next month: if(year(now()) = year("my_date"), month("my_date") - month(now()), month("my_date") - month(now()) + (12 * (year("my_date") - year(now())))) = 1
  • second next month: if(year(now()) = year("my_date"), month("my_date") - month(now()), month("my_date") - month(now()) + (12 * (year("my_date") - year(now())))) = 2
  • third next month: if(year(now()) = year("my_date"), month("my_date") - month(now()), month("my_date") - month(now()) + (12 * (year("my_date") - year(now())))) = 3
  • forth next month: if(year(now()) = year("my_date"), month("my_date") - month(now()), month("my_date") - month(now()) + (12 * (year("my_date") - year(now())))) = 4
  • fifth next month: if(year(now()) = year("my_date"), month("my_date") - month(now()), month("my_date") - month(now()) + (12 * (year("my_date") - year(now())))) = 5
  • beyond 5 months: if(year(now()) = year("my_date"), month("my_date") - month(now()), month("my_date") - month(now()) + (12 * (year("my_date") - year(now())))) > 5

Tags:

Qgis

Style