SoFunction
Updated on 2025-04-08

Let’s talk about the various methods of formatting date strings in DOS batch processing (detailed explanation)

In Linux, a simple sentence: date '+%Y%m%d-%H%M%S' can get the output you want: 20120331-064219
However, under Windows, it still takes some effort to get this.

1. Directly format the output with strings
For example, if the output of time on your machine looks like this:
c:\>echo %date%-%time%
2012-03-31 Saturday - 6:44:02.50
Then, to get the output 20120331-64402, you can intercept the string like this:
c:\>echo %date:~0,4%%date:~5,2%%date:~8,2%-%time:~0,2%%time:~3,2%%time:~6,2%
20120331- 65406
It means to take 4 characters from position 0, etc. This method cannot truncate spaces. More annoying

2. Use the for statement to break and intercept, it seems to be better
Let’s take a look at the usage 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) // Ignore the line that starts with which character
skip=n - refers to the number of lines ignored at the beginning of the file.
delims=xxx - refers to the separator set. This replaces the space and jump keys // specified split characters in delims=;:. Use ";",":",." for separation
The default separator set.
tokens=x,y,m-n - refers to which symbol of each line is passed to each iteration
for itself. This results in the allocation of additional variable names. m-n
The format is a range. Specify mth by the nth symbol. if
The last character asterisk in the symbol string,
Then the extra variable will be parsed after the last symbol
Allocate and accept reserved text for rows.
usebackq - Specifies that the new syntax has been used in the following class:
Execute a quoted string as a command and a single
Quotation characters are literal string commands and are allowed in filenameset
Use double quotes to extend the file name.
Referring to this usage, we can format the date string and output:
Because the date result is: 2012-03-31 Saturday
Because the separator character is '-' and space' ', take 3 segments, such as:
c:\>for /f "tokens=1-3 delims=- " %1 in ("%date%") do @echo %1%2%3
20120331
Look at the time again:
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 like this:

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
Written into batch processing, it becomes like this ():
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 type is to use VBScript to customize the output
year(date) & right( "0" & month(date),2) & right( "0" & day(date),2) & "-" & right( "0" & hour(time),2) & right( "0" & minute(time),2)

The result is as follows, it is quite interesting
c:\>cscript /nologo c:\shared\
20120331-0711
Of course, this is still not ideal. You can write a batch and get the results into the environment variables:
, the content is as follows:
@echo off
cscript /nologo >>
for /f "tokens=*" %%1 in () do set goodtime=%%1
echo goodtime=%goodtime%
To sum up, the third method is the most complex. It is not possible to determine whether it can run correctly on all Windows platforms, but the result should be fixed. It does not change with the change of date and time format. But 1 and 2 will depend on the date and time format. That's all.
It's still relatively fixed under Linux.

This article comes from iihero laboratory