I need to send a date to a batch file and have it return the day of the week. The date parameter could be in the past or in the future. I have looked through a number of date functions but they are all using today's date. Is it possible to return the day of the week by sending a date parameter to a batch file? I am using Windows 7.
Asked
Active
Viewed 1,604 times
2
-
are you really constrained to pure batch? What you ask is very easy in powershell (you may call it from cmd/batch): `powershell date('2014-05-18') -Uformat %V` – wmz Apr 18 '14 at 19:00
-
sorry it should be `%u` I misread you want week not day. This returns number, there are other options available as well – wmz Apr 18 '14 at 19:07
-
Actually, I need the day of the week. For example: Mon, Tue. Unfortunately, I do need it to remain in batch format. – user3549841 Apr 18 '14 at 19:13
-
that would be `%a`(or use .net format as Stephan shows), for invocation from batch see his answer too – wmz Apr 18 '14 at 19:27
-
Possible duplicate of [Setting a windows batch file variable to the day of the week](https://stackoverflow.com/questions/11364147/setting-a-windows-batch-file-variable-to-the-day-of-the-week) – phuclv Jun 05 '18 at 01:58
3 Answers
2
with the help of Powershell:
set a=03.12.2013
for /f %%i in ('powershell ^(Get-Date %a% -f dddd^)') do set long=%%i
for /f %%i in ('powershell ^(Get-Date %a% -f ddd^)') do set short=%%i
echo Day is %long% (%short%)

Stephan
- 53,940
- 10
- 58
- 91
1
save this as .bat
and change the date1
to the date you want
@if (@X)==(@Y) @end/*** Javascript comment
@echo off
set "date1=04/14/2014"
(
mshta "about:<title>dow</title><body onload='init("%date1%")'><script language='javascript' src='file://%~dpnxf0'></script><span id='arg1'>%date1%</span></body>"
) | (
for /f "tokens=* delims=" %%B in ('more') do (
echo day of the week: %%B
)
)
exit /b 0
**/
function init(ds) {
//var dt = document.getElementById('arg1').value;
var the_date=new Date(Date.parse(ds));
var the_day=the_date.getDay();
//alert(the_day);
var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
//fso.Write(the_date.getDay());
close(fso.Write(the_day));
}

npocmaka
- 55,367
- 18
- 148
- 187
1
This is a batch script using VBS to get the weekday.
:: Get weekday and day of week number, for a date
@echo off
if "%~1"=="" echo Use this to get the day of week: "%~nx0" dd-mm-yyyy& pause & goto :EOF
set file="%temp%\%~n0.temp.vbs"
echo>%file% WScript.Echo weekday(#%~1#)
for /f %%a in ('cscript //nologo %file%') do set "daynum=%%a"
del %file%
for /f "tokens=%daynum% delims=," %%a in ("Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday") do set "weekday=%%a"
echo dow is "%daynum%" - "%weekday%"
pause

foxidrive
- 40,353
- 10
- 53
- 68