Several methods of formatting date string under DOS batch processing

Time:2020-2-26

Under Linux, a simple sentence: date ‘+% Y% m% d -% H% m% s’ can get the output you want: 20120331-064219
But in windows, it takes a lot of work to get this.

1. Format output directly with string
For example, if the output of time on your machine is like this:
c:\>echo %date%-%time%
Saturday, March 31, 2012 – 6:44:02.50
Then, to get the output 20120331 – 64402, you can intercept the string as follows:
c:\>echo %date:~0,4%%date:~5,2%%date:~8,2%-%time:~0,2%%time:~3,2%%time:~6,2%
20120331- 65406 www.jb51.net
It means to take 4 characters from position 0 and so on. This method cannot truncate spaces. More annoyed

2. It seems better to use the for statement to break and intercept
First look at the use of for:
FOR /F [“options”] %variable IN (file-set) DO command [command-parameters]
FOR /F [“options”] %variable IN (“string”) DO command [command-parameters]
FOR /F [“options”] %variable IN (‘command’) DO command [command-parameters]
optinos
EOL = C – refers to the end of a line comment character (just one) / / the line starting with what character should be ignored
Skip = n – number of lines ignored at the beginning of the file.
Delims = XXX – refers to the separator set. This / / character with space and skip key replaced is listed in delims =;:. It is separated by “;”, “:”, “”
Default separator set.
Tokens = x, y, M-N – which symbol of each line is passed to each iteration
For itself. This results in the assignment of additional variable names. M-N
The format is a range. Mth is specified by the nth symbol. If
The asterisk of the last character in the symbol string,
Then the extra variables will be parsed after the last symbol
Assign and accept reserved text for the line.
Usebackq – specifies that the new syntax has been used in the following class cases:
After executing a quoted string as a command and a single
Quote characters are text string commands and are allowed in filenameset
Encloses the file name in double quotes.
Referring to this usage, we can format and output the date string:
Because the result of the date is: Saturday, March 31st, 2012
Because the separator character is’ – ‘and space’, take three segments, such as:
c:\>for /f “tokens=1-3 delims=- ” %1 in (“%date%”) do @echo %1%2%3
20120331 www.jb51.net
Time:
c:\>echo %time%
6:59:20.38
c:\>for /f “tokens=1-3 delims=.: ” %1 in (“%time%”) do @echo %1%2%3
65939
The combination of the two can be used as follows:

c:\>for /f “tokens=1-3 delims=- ” %1 in (“%date%”) do set ttt=%1%2%3
c:\>set ttt=20120331
c:\>for /f “tokens=1-3 delims=.: ” %1 in (“%time%”) do set ttt=%ttt%-%1%2%3
c:\>set ttt=20120331-70131
When it is written as a batch, it becomes like this (goodtime. Bat):
for /f “tokens=1-3 delims=- ” %%1 in (“%date%”) do set ttt=%%1%%2%%3
for /f “tokens=1-3 delims=.: ” %%1 in (“%time%”) do set ttt=%ttt%-%%1%%2%%3
echo goodtime=%ttt%
This method is more flexible.

3. Another way is to use VBScript to customize the output
wscript.echo year(date) & right( “0” & month(date),2) & right( “0” & day(date),2) & “-” & right( “0” & hour(time),2) & right( “0” & minute(time),2)

The results are as follows, which is more interesting
c:\>cscript /nologo c:\shared\datescript.vbs
20120331-0711
Of course, this is not ideal. You can write a batch to get the results into the environment variables:
Mydate2.bat, as follows:
@echo off
cscript /nologo datescript.vbs >> tmp.txt
for /f “tokens=*” %%1 in (tmp.txt) do set goodtime=%%1
echo goodtime=%goodtime%
In summary, the third method is the most complex, and it is not sure whether it can run correctly in all windows platforms at present, but the result should be fixed. It does not change with the date and time formats. But 1 and 2 will depend on the date time format. That’s it.
It’s still fixed under Linux.

This paper is from iihero Laboratory