Why does %date% produce a different result in batch file executed as scheduled task?

%date% or date /t isn’t always reliable as it depends on region and user settings. Here’s how you can get a universally valid date:

for /f %%# in ('wmic os get localdatetime^|findstr .') do if "%%#" neq "" set date=%%#
set date=%date:~,4%-%date:~4,2%-%date:~6,2%
echo %date%

This will echo the current date in the yyyy-mm-dd format.

Leave a Comment