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.
Asked
Active
Viewed 76 times
-2
-
1I don't see a question here. If you're looking for improvements to your working code, please ask in [Code Review](https://codereview.stackexchange.com/) instead. Otherwise, please move your solution to the Answers section of this page and rewrite your question to be an actual question. – SomethingDark Jul 18 '23 at 01:26
-
1This is not an on topic question. What you are expected to do is post a specific programming problem. Then if you wish to self answer, you submit that answer in the appropriate location. I have therefore removed all of your answer content from your question. As a result your question no longer makes sense as a request for programming code assistance. Please improve it, before submitting your answer. – Compo Jul 18 '23 at 01:30
2 Answers
1
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"...

Aacini
- 65,180
- 12
- 72
- 108
-
The first `for` command line must be `for /F "tokens=1-3 delims=." %%a in ("%date%") do set /A "D=1%%a-100,M=1%%b-100,Y=%%c"` for a German date like `18.07.2023` with a dot as separator instead of a slash and day before month in date string. A German user should use also German weekday names and the German verb `ist` instead of English verb `is`. – Mofi Jul 18 '23 at 09:56
1
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

Hackoo
- 18,337
- 3
- 40
- 70
-
1That is also nice and works even on English Windows 7 with PowerShell 2.0. It outputs the weekday in the language configured for the user account which is German in my case while the OS language is English. – Mofi Jul 18 '23 at 10:01