I NEED THE PRECISE DATE FOR MY BATCH FILE

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
G.SYLVAIN
Posts: 6
Joined: 30 Oct 2018 19:52

I NEED THE PRECISE DATE FOR MY BATCH FILE

#1 Post by G.SYLVAIN » 30 Oct 2018 20:02

This batch file does not work for the days of the week. Could you help me ?
Excuse me for my capital letters in the batch file. I find it more formal and visible

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

	FOR /F %%I IN ('WMIC OS GET LOCALDATETIME /FORMAT:LIST ^| FIND "="') DO SET "%%I"
	SET "YYYY=%LOCALDATETIME:~0,4%"
	SET /A "MM=1%LOCALDATETIME:~4,2% - 100"
	SET "DD=%LOCALDATETIME:~6,2%"
	::SET I=

CALL :MONTHS I
CALL :DAYS   J

ECHO.
ECHO           TODAY IS %DAYS% %DD% %MONTHS%, %YYYY%
ECHO.
PAUSE>nul
::
EXIT

:MONTHS
::SETLOCAL
FOR /F "TOKENS=%MM%" %%I IN ("JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER OCTOBER NOVEMBER DECCEMBER") DO SET "MONTHS=%%I"
::ENDLOCAL
EXIT /B

:DAYS
::SETLOCAL
FOR /F "TOKENS=%DD%" %%J IN ("MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY") DO SET "DAYS=%%J"
::ENDLOCAL
EXIT /B

::ECHO A %MONTHS% 
::PAUSE
::CALL :DAYS  I 

Squashman
Expert
Posts: 4113
Joined: 23 Dec 2011 13:59

Re: I NEED THE PRECISE DATE FOR MY BATCH FILE

#2 Post by Squashman » 30 Oct 2018 20:40

You can use WMIC to get the day of week. It will output as a number.

Code: Select all

wmic path win32_localtime get dayofweek
Sunday=0
Monday=1
Tuesday=2
etc......

Although it might be easier to use Powershell instead.

Code: Select all

C:\Users\Squashman>powershell (get-date).DayOfWeek
Tuesday

G.SYLVAIN
Posts: 6
Joined: 30 Oct 2018 19:52

Re: I NEED THE PRECISE DATE FOR MY BATCH FILE

#3 Post by G.SYLVAIN » 31 Oct 2018 07:59

I solved the problem. I cleaned the code. Thanks to you

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

	FOR /F %%I IN ('WMIC OS GET LOCALDATETIME /FORMAT:LIST ^| FIND "="') DO SET "%%I"
	SET "YYYY=%LOCALDATETIME:~0,4%"
	SET /A "MM=1%LOCALDATETIME:~4,2% - 100"
	SET "DD=%LOCALDATETIME:~6,2%"
	FOR /F %%D IN ('WMIC PATH WIN32_LOCALTIME GET DAYOFWEEK ^| FINDSTR [1-7]') DO SET "DA=%%D"
	
CALL :MONTHS I
CALL :DAYS   DAYS

ECHO.
ECHO           TODAY IS %DAYS% %DD% %MONTHS%, %YYYY%
ECHO.
PAUSE>nul
::
EXIT

:MONTHS
::
FOR /F "TOKENS=%MM%" %%I IN ("JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER OCTOBER NOVEMBER DECCEMBER") DO SET "MONTHS=%%I"
EXIT /B
::
:DAYS
::
FOR /F "TOKENS=%DA%" %%D IN ("MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY") DO SET "DAYS=%%D"
::
EXIT /B

aGerman
Expert
Posts: 3744
Joined: 22 Jan 2010 18:01
Location: Germany

Re: I NEED THE PRECISE DATE FOR MY BATCH FILE

#4 Post by aGerman » 31 Oct 2018 09:54

Did you note that Squashman wrote DayOfWeek is in the interval 0 (for Sunday) to 6 (for Saturday)?

Code: Select all

@echo off &setlocal
for /f %%i in ('WMIC PATH WIN32_LOCALTIME GET /Value') do for /f %%j in ("%%i") do set "%%j"
set /a "DayOfWeek+=1"
for /f "tokens=%DayOfWeek%" %%i in ("SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY") do set "DayOfWeek=%%i"
for /f "tokens=%Month%" %%i in ("JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER OCTOBER NOVEMBER DECEMBER") do set "Month=%%i"
echo TODAY IS %DayOfWeek% %Day% %Month%, %Year%
pause
Steffen

Squashman
Expert
Posts: 4113
Joined: 23 Dec 2011 13:59

Re: I NEED THE PRECISE DATE FOR MY BATCH FILE

#5 Post by Squashman » 31 Oct 2018 12:52

And one line of code if you call out to Powershell.

Code: Select all

powershell "(Get-Date).ToString('TODAY IS dddd dd MMMM yyyy')"

Post Reply