Build_flags on Windows machine

I used to use in platformio.ini something like this to get current date:

build_flags=-D FW_VERSION=%date:~2,2%%date:~5,2%%date:~8,2%

It returned me two digits of year, month and day.
Recently it stopped worked after some platformio upgrade
I’m receiving error

Error: Invalid ‘C:\priv\platformio\sdmoto\platformio.ini’ (project configuration file): ‘’%’ must be followed by ‘%’ or ‘(’, found: ‘%date:~2,2%%date:~5,2%%date:~8,2%’’

How can I fix it?
Thank you

So have you tried surrounding it with ( as it wants?

Seems like you’re trying to do a cmd.exe inline command evaluation when the compiler is executed. It would be much more safer, cleaner and cross-platform to use advanced scripting for that.

E.g., add extra_scripts = to the platformio.ini

and in at the root of the project

import datetime

curr_date =
year = str(curr_date.year)[2:] # ignore first 2 chars of year; 2020 -> 20
# code it in DDMMY
# day and month are zero-padded to length 2; e.g. 021120 -> 02.11.2020
date_str = f"{}{curr_date.month:02}{year}"
# append integer value to global defines
  ("FW_VERSION", int(date_str))

Thank you for the answer.
I already tried many combination with percent, parentheses, escape characters and nothing works.
-D FW_VERSION=%(date)%
gives me
‘bad interpolation variable reference ‘%(date)%’’

Maybe there is a magic combination of characters that makes parser happy, but I can’t get it.
I’m absolutely sure that my method worked before some recent platformio upgrade. Just want to avoid having extra scripts necessary to compile sources.
I can use
but it require math to convert to YYMMDD

Well the question is here what was updated that broke it (though I don’t think command injection wasn’t intended in the first place). If it’s the PIO core that handles compilation differently, then, depending on how you installed it, you can install a different version, with e.g. pip in a PIO terminal, using pip uninstall platformio and pip install 'platformio==<some version>' commands with version from here.

The other thing that could have update is the “platform”, e.g. platform-atmelavr or whatever platform you’re compiling for. You can change the platform using e.g. platform = atmelavr@<version> commands with a version from the release page of the platform (example).

The Python advanced scripting is cross-platform for all operating systems and PIO is always installed on top of Python or (in the VSCode Plugin case) even brings its own Python3 interpreter. So that will always work. It’s the best option in my opinion, rather than depending on a command injection.

Hm actually I’m wrong on this one. It is explicitly mentioned in Redirecting....

Have you tried some syntax forms from that page?

Yes, I already tried to put in build_flags:
!echo -DFW_VERSION=%date%

Same error here complaining about percent character.
Apparently it works different than in regular command line:

C:\Windows\System32>echo -DFW_VERSION=%date:~2,2%%date:~5,2%%date:~8,2%