When working with batch scripts on Windows Server 2008, many developers encounter the problem of retrieving the current day of the week in a reliable way. The standard approach using date /t
fails when regional settings don't include weekday information in the short date format.
Windows date formatting is locale-dependent. While some systems display "Friday, 12/11/2009" for date /t
, others (especially in enterprise environments) show just "12/11/2009". This becomes problematic for scheduling scripts that need to run on specific days.
Here are three reliable methods to get the weekday in batch files:
Method 1: Using WMIC (Recommended)
@echo off
for /f "tokens=2 delims==" %%A in ('wmic path win32_localtime get dayofweek /value') do (
set "dow=%%A"
)
set /a dow=%dow% + 1
echo Day of week (0=Sun,6=Sat): %dow%
Method 2: PowerShell Integration
@echo off
for /f %%A in ('powershell -command "(Get-Date).DayOfWeek.value__"') do (
set "dow=%%A"
)
echo Day of week (0=Sun,6=Sat): %dow%
Method 3: Date Parsing with Regional Independence
@echo off
for /f "tokens=1-3 delims=/ " %%A in ('date /t') do (
set "mm=%%A"
set "dd=%%B"
set "yyyy=%%C"
)
:: Use Zeller's Congruence algorithm
set /a "a=(14-mm)/12"
set /a "y=yyyy-a"
set /a "m=mm+12*a-2"
set /a "dow=(dd+y+y/4-y/100+y/400+(31*m)/12)%%7"
echo Day of week (0=Sun,6=Sat): %dow%
Here's how to run a process only on Fridays:
@echo off
:: Get day of week (0=Sun,6=Sat)
for /f %%A in ('powershell -command "(Get-Date).DayOfWeek.value__"') do set "dow=%%A"
if %dow% equ 5 (
echo It's Friday! Running scheduled process...
rem Your commands here
) else (
echo Not Friday (day %dow%), skipping execution
)
The WMIC method is native but relatively slow (200-400ms). PowerShell is faster (100-200ms) but requires PS to be available. The mathematical approach is fastest but most complex. Choose based on your environment constraints.
When deploying across multiple servers, test all methods as some environments might restrict WMIC or PowerShell execution. The mathematical approach, while complex, has the benefit of working in locked-down environments.
Many batch script examples rely on date /t
outputting the weekday (e.g., "Friday, 12/11/2009"). However, this fails when regional settings use short date format. On Windows Server 2008, you might only get "12/11/2009" without the weekday.
The most reliable method uses WMIC (Windows Management Instrumentation Command-line):
@echo off
for /f "tokens=2 delims==" %%A in ('wmic path win32_localtime get dayofweek /value') do (
set DOW=%%A
)
set DOW=%DOW:~0,-1%
if %DOW% equ 0 (
echo Sunday
) else if %DOW% equ 1 (
echo Monday
) else if %DOW% equ 2 (
echo Tuesday
) else if %DOW% equ 3 (
echo Wednesday
) else if %DOW% equ 4 (
echo Thursday
) else if %DOW% equ 5 (
echo Friday
) else if %DOW% equ 6 (
echo Saturday
)
For more complex scenarios, consider calling PowerShell from your batch:
@echo off
for /f %%A in ('powershell -command "(Get-Date).DayOfWeek"') do (
set DOW=%%A
)
if "%DOW%"=="Friday" (
echo Running Friday-specific process
:: Your commands here
)
The WMIC method returns numeric values (0-6) making it locale-independent. Here's a compact version:
@echo off
for /f %%A in ('wmic path win32_localtime get dayofweek ^| findstr [0-6]') do set DOW=%%A
:: Example: Run only on Wednesday (DOW=3)
if %DOW% equ 3 (
call :wednesday_process
)
For reusable code, create a function:
@echo off
call :get_day_of_week
if "%DOW_NAME%"=="Friday" (
echo It's Friday! Running special tasks...
)
exit /b
:get_day_of_week
for /f %%A in ('wmic path win32_localtime get dayofweek ^| findstr [0-6]') do set DOW=%%A
set DOW_NAMES=Sunday Monday Tuesday Wednesday Thursday Friday Saturday
call set DOW_NAME=%%DOW_NAMES:~%DOW%*%%
call set DOW_NAME=%%DOW_NAME:~0,7%%
goto :eof