Page 1 of 1

Scripts to get hour not always correct

Posted: 20 Mar 2022 07:50
by goodywp
Hi All,

I have a script to divide the hour into two periods, from 10-21 set not run, the rest set run as below:

Code: Select all

SET hour=%time:~0,2%
echo %hour%
IF %hour% leq 21 (
	IF %hour% geq 10 (
			SET shouldrun=False
			echo 1
			) else (
			SET shouldrun=True
			echo 2
			)
	) else (
	set shouldrun=True	
	echo 3
	)

if "%shouldrun%" == "True" goto to_run
if "%shouldrun%" == "False" goto not_run

:to_run

echo run

goto end

:not_run
echo not run

:end
But from the log file the time is not always correct, some case supposed to be run shows not run....
I know hour from time usually 24 format. please correct me if I am wrong.
Any idea?

Re: Scripts to get hour not always correct

Posted: 20 Mar 2022 17:25
by Compo
The first thing you need to understand is that %TIME% is locale, PC, and user configurable and may not therefore return a string in the format you require for splitting, and even if it is, that may not be returned as a 24 hour clock string.

There are many ways of retrieving the time in a consistent format, and I would advise that you choose one such alternative.

The following method, which works on supported Operating Systems pre-Windows 11 uses WMI via their built-in WMIC executable, and could achieve your task in just a single line batch file:

Code: Select all

@%SystemRoot%\System32\wbem\WMIC.exe Path Win32_LocalTime Where "Hour < 10 Or Hour > 21" Assoc 2>NUL | %SystemRoot%\System32\find.exe ":" && Echo run
Alternatively you could use supported Operating System's built-in PowerShell executable, and still do it with a one line batch file:

Code: Select all

@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoL -NoP "[Int]$hr=Get-Date -Format 'HH'; If ($hr -lt 10 -or $hr -gt 21) { Exit 0 }" && Echo Run

Re: Scripts to get hour not always correct

Posted: 22 Mar 2022 14:07
by goodywp
Thanks Compo! It works....

Re: Scripts to get hour not always correct

Posted: 29 Apr 2022 08:07
by goodywp
Compo wrote:
20 Mar 2022 17:25
The first thing you need to understand is that %TIME% is locale, PC, and user configurable and may not therefore return a string in the format you require for splitting, and even if it is, that may not be returned as a 24 hour clock string.

There are many ways of retrieving the time in a consistent format, and I would advise that you choose one such alternative.

The following method, which works on supported Operating Systems pre-Windows 11 uses WMI via their built-in WMIC executable, and could achieve your task in just a single line batch file:

Code: Select all

@%SystemRoot%\System32\wbem\WMIC.exe Path Win32_LocalTime Where "Hour < 10 Or Hour > 21" Assoc 2>NUL | %SystemRoot%\System32\find.exe ":" && Echo run
Alternatively you could use supported Operating System's built-in PowerShell executable, and still do it with a one line batch file:

Code: Select all

@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoL -NoP "[Int]$hr=Get-Date -Format 'HH'; If ($hr -lt 10 -or $hr -gt 21) { Exit 0 }" && Echo Run
Hi Compo,
Now I got the issues if I have more than two time slices as below:

Code: Select all

@%SystemRoot%\System32\wbem\WMIC.exe Path Win32_LocalTime Where "Hour > 8 Or Hour < 14" Assoc 2>NUL | %SystemRoot%\System32\find.exe ":" && SET shouldrun=Jum
@%SystemRoot%\System32\wbem\WMIC.exe Path Win32_LocalTime Where "Hour > 14 Or Hour < 20" Assoc 2>NUL | %SystemRoot%\System32\find.exe ":" && SET shouldrun=True
@%SystemRoot%\System32\wbem\WMIC.exe Path Win32_LocalTime Where "Hour > 20 Or Hour < 8" Assoc 2>NUL | %SystemRoot%\System32\find.exe ":" && SET shouldrun=False
echo %shouldrun%
I tried on 9:00AM and it echo True which is within "Hour > 14 Or Hour < 20", what is wrong here? It supposed to be Jum ("Hour > 8 Or Hour < 14" )

Re: Scripts to get hour not always correct

Posted: 29 Apr 2022 08:39
by atfon
Have a look at this post from aGerman where he explains how to set a clock to AM/PM: viewtopic.php?f=3&t=2051#p64840