0

When I print the actual time in a .bat file I use

 TIME /T

but the output is like this:

 04:30 a. m.   or  14:30 p. m.

I'd like to know if it's possible to remove the ":" between the numbers and the a. m. / p. m. from the end of it, so the output looks this way:

 0430 or 1430

Also, I'd like to know if it's possible to compare that output or the time itself (time /t). If it's lower than X time, an action is triggered. I don't know if this code is correct, but something like this:

 set /a time=TIME /T     
 if %time%<=(01:00 a. m.) (GOTO 0am)
 if %time%<=(02:00 a. m.) (GOTO 1am)
 if %time%<=(03:00 a. m.) (GOTO 2am)
 if %time%<=(04:00 a. m.) (GOTO 3am)
 if %time%<=(05:00 a. m.) (GOTO 4am)
 if %time%<=(06:00 a. m.) (GOTO 5am)
 if %time%<=(07:00 a. m.) (GOTO 6am)
 if %time%<=(08:00 a. m.) (GOTO 7am)
 if %time%<=(09:00 a. m.) (GOTO 8am)
 if %time%<=(10:00 a. m.) (GOTO 9am)
 if %time%<=(11:00 a. m.) (GOTO 10am)
 if %time%<=(12:00 a. m.) (GOTO 11am)
 if %time%<=(13:00 a. m.) (GOTO 12am)
 if %time%<=(14:00 a. m.) (GOTO 1pm)
 if %time%<=(15:00 a. m.) (GOTO 2pm)
 if %time%<=(16:00 a. m.) (GOTO 3pm)
 if %time%<=(17:00 a. m.) (GOTO 4pm)
 if %time%<=(18:00 a. m.) (GOTO 5pm)
 if %time%<=(19:00 a. m.) (GOTO 6pm)
 if %time%<=(20:00 a. m.) (GOTO 7pm)
 if %time%<=(21:00 a. m.) (GOTO 8pm)
 if %time%<=(22:00 a. m.) (GOTO 9pm)
 if %time%<=(23:00 a. m.) (GOTO 10pm)
 if %time%<=(24:00 a. m.) (GOTO 11pm)

If you know an easier way to do this, please let me know.

Thanks for your time.

soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
  • It would be nice if you FIRST rtm. See `Help set` and `Help if`, or visit [ss64.com/nt](http://ss64.com/nt/) –  Dec 27 '16 at 18:12

2 Answers2

0

In order to get the time formatting you are looking for you will need to use batch scripting string manipulation. This can be seen in this example Format date and time in a Windows batch script.

I believe something like this:

set time=%time:a. m.=%

would remove a. m. from the time. The trick will be to remove the : since it is part of the string manipulation syntax.

Another good reference for batch string manipulation DOS - String Manipulation

soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
  • Thanks man! It's possible to compare a number with ":" in between? –  Dec 27 '16 at 18:18
  • I do not believe so. The best approach I can think of is to use "set time=%time::=%" after you remove the am pm characters. – soundslikeodd Dec 27 '16 at 19:32
0

I suggest first to open a command prompt window, run set /? and read all output help pages. The last help page is about built-in environment variables for usage in batch files and of course also on command line to which also DATE and TIME belongs.

The format of DATE and TIME strings depends on Windows Region and Language settings as defined for the user account running the batch file using the environment variables DATE and TIME.

For that reason it is sometimes better to get date and time string in a region independent format which can be achieved by using the command

wmic OS GET LocalDateTime /VALUE

which outputs for example

LocalDateTime=20161227200325.171000+060

The date and time string format can be easily seen on looking on this string.

To get from this string just current hour and minute without a colon between in 24 hours format with leading zeros for hours < 10 and minutes < 10 the following batch code can be used:

@echo off
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalTime=%%I"
set "LocalTime=%LocalTime:~8,4%"
echo Local time in format HHMM is: %LocalTime%
pause

The output of WMIC is split up by FOR into 3 strings using = and . as delimiters as defined with delims==.. Of interest for this task is only the second string with date and time referenced with tokens=2 which is assigned to loop variable I. The date/time string in international format is assigned to environment variable LocalTime.

The first 8 characters are for the date in format yyyymmdd which is not of interest as well as the last 2 characters which are for the current second. Therefore from LocalTime just the 4 characters starting from character index position 8 are assigned next to LocalTime before outputting the resulting time string.

The output of the batch file for the posted WMIC output is:

Local time in format HHMM is: 2003

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • for /?
  • pause /?
  • set /?
  • wmic /?
  • wmic OS /?
  • wmic OS GET /?
Mofi
  • 46,139
  • 17
  • 80
  • 143