3

I'm wondering if I can somehow figure out what DAY of the week it is (M,T,W,T,F,S & S) though some sort of batch file. It's fine if it uses any other language, just as long as in the end, the variable %DAY% it set to the day of the week. Is there any way possible to accomplish this? Any help would be greatly appreciated! Thanks.

drcomputer
  • 406
  • 2
  • 7
  • 15

2 Answers2

3

You could toss this in a batch file.

@echo off  
set daysofweek=Mon,Tues,Wed,Thurs,Fri,Sat,Sun  
for /F "skip=2 tokens=2-4 delims=," %%A in ('WMIC Path Win32_LocalTime Get DayOfWeek /Format:csv') do set daynumber=%%A  
for /F "tokens=%daynumber% delims=," %%B in ("%daysofweek%") do set day=%%B
echo %day%
JustSomeQuickGuy
  • 933
  • 1
  • 10
  • 21
  • Thanks you very much! Works! Does that require admin on XP? Weird. At school works on some XP machines an all 7 machines. Do you know anything about this? – drcomputer Feb 12 '14 at 03:53
  • This is using the WMI service via the WMI command line, so it may be that it wasn't running on those machines? – JustSomeQuickGuy Feb 12 '14 at 04:01
1

As wmic is not available no home editions of windows:

@echo off
setlocal
rem :: prints the day of the week
rem :: works on Vista and above
rem :: uses W32tm command

    rem :: getting ansi date ( days passed from 1st jan 1601 ) , timer server hour and current hour 
    FOR /F "skip=16 tokens=4,5 delims=:( " %%D in ('w32tm /stripchart /computer:localhost  /samples:1  /period:1 /dataonly /packetinfo') do (
     set "ANSI_DATE=%%D" 
     set  "TIMESERVER_HOURS=%%E" 
     goto :end_for  )
    :end_for
    set  "LOCAL_HOURS=%TIME:~0,2%"
    if "%TIMESERVER_HOURS:~0,1%0" EQU "00" set TIMESERVER_HOURS=%TIMESERVER_HOURS:~1,1%
    if "%LOCAL_HOURS:~0,1%0" EQU "00" set LOCAL_HOURS=%LOCAL_HOURS:~1,1%
    set /a OFFSET=TIMESERVER_HOURS-LOCAL_HOURS

    rem :: day of the week will be the modulus of 7 of local ansi date +1 
    rem :: we need need +1 because Monday will be calculated as 0
    rem ::  1st jan 1601 was Monday

    rem :: if abs(offset)>12 we are in different days with the time server

    IF %OFFSET%0 GTR 120 set /a DOW=(ANSI_DATE+1)%%7+1 
    IF %OFFSET%0 LSS -120 set /a DOW=(ANSI_DATE-1)%%7+1
    IF %OFFSET%0 LEQ 120 IF %OFFSET%0 GEQ -120 set /a DOW=ANSI_DATE%%7+1 


    echo Day of the week: %DOW%
    exit /b 2147483648
endlocal
npocmaka
  • 55,367
  • 18
  • 148
  • 187