Easy way to get the weekday in a batch file

I was searching for a good easy answer to getting the day of the week from %date% and saw a lot of answers but none very eloquent.

Upvotes: 0

Views: 1249

Answers (3)

Andreas Lanz
Andreas Lanz

Reputation: 1

@echo off
for /f %%i in ('powershell ^(get-date^).DayOfWeek') do set dow=%%i
echo %dow%

Upvotes: 0

Hackoo
Hackoo

Reputation: 18857

Another solution using Powershell command :

@echo off
Title Get and Show The day of the week
@for /f "delims=" %%a in ('Powershell -C "(Get-Date).ToString('dddd')"') do Set "DayOfWeek=%%a"
echo( The day of the Week is %DayOfWeek%
Pause

Upvotes: 2

Aacini
Aacini

Reputation: 67256

You ask for it, so here it is!

@echo off
setlocal

for /F "tokens=1-3 delims=/" %%a in ("%date%") do set /A "M=1%%a-100,D=1%%b-100,Y=%%c"
set /A "DOW=( D + 30*(M-1)+M/2+(M>>3)*(M&1) - !(((2-M)>>31)+1)*(2-!(Y%%4)) +6)%%7+1"
for /F "tokens=%DOW%" %%a in ("Sunday Monday Tuesday Wednesday Thursday Friday Saturday") do echo %date% is %%a

This is a "good easy answer to getting the day of the week from %date%". However, I'm afraid I don't understand what "very eloquent" means... ;)

This formula assumes that %date% show the date in MM/DD/YYYY format. If the order is a different one, just change the M and D variables in the formula.

This method requires a small adjustment every new year: just change the +6 adjustment to a value that get the right day of week on Jan/1st of the new year (the value must be between 0 and 6). This is done in order to keep the method "simple and easy"...

Upvotes: 3

Related Questions