Page 1 of 1

[SOLVED] Extract the named day of the week.

Posted: 01 Nov 2019 14:32
by PAB
Good evening,

I would like to output the day of the week, the date, and the time.

This works exactly as I want for the date and the time . . .

Code: Select all

echo Created: %date% at %time:~0,2%:%time:~3,2%:%time:~6,2%
This does what I want but puts the day number in a two digit format after the named day of the week which is not what I want . . .

Code: Select all

Set daysofweek=Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday  
For /f "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 Created: %day% the %date:~0,3% %date% at %time:~0,2%:%time:~3,2%:%time:~6,2%
For example, for today it should show Friday the 01/11/2019 at whatever time.

Thanks in advance.

Re: Extract the named day of the week.

Posted: 01 Nov 2019 16:09
by aGerman

Code: Select all

@echo off &setlocal
for /f %%i in ('WMIC OS GET LocalDateTime /value ^& WMIC Path Win32_LocalTime GET DayOfWeek /value') do for /f %%j in ("%%i") do set "%%j"
set /a "tok=DayOfWeek+1"
for /f "tokens=%tok%" %%i in ("Sunday Monday Tuesday Wednesday Thursday Friday Saturday") do set "day=%%i"
echo Created: %day% the %LocalDateTime:~6,2%/%LocalDateTime:~4,2%/%LocalDateTime:~0,4% at %LocalDateTime:~8,2%:%LocalDateTime:~10,2%:%LocalDateTime:~12,2%
Probably I reinvented the wheel with this code. No idea how many threads already contain similar snippets.

Steffen

Re: Extract the named day of the week.

Posted: 01 Nov 2019 16:27
by PAB
Thank you. Why does it only appear in the cmd window if I put Pause after the last line please?

Re: Extract the named day of the week.

Posted: 01 Nov 2019 17:24
by aGerman
Why does it only appear in the cmd window if I put Pause after the last line please?
It appears anyway. The pause command prevents the script window from closing immediately though.

Steffen

Re: Extract the named day of the week.

Posted: 01 Nov 2019 18:13
by PAB
Thank you Steffen, it is appreciated. Have a great weekend!