Batch script application - print the current date and time

basis

Windows provides a lower print time and date of the application:

We want to get the current date and time will need to use these.

These two commands can also be used to display settings for our current needs, it is to gain, so it is necessary to increase / T, of course, this requires "Command Extensions are enabled," if that is enabled by default. How open or closed, as follows:

 

String interception

Let's look at the date and time display information:

How was the result of a combination of two command becomes the current date and time we need it?

Here it is necessary to use an intercept of the string, the following is an example:

String interception process is part of a string, string processing using% xx: yy%.

xx: yy two parts, or can generate a string in front of the command string (such as date / time, etc.), followed by a string processing, the above script using the "~ 0" indicates that all the string, can also take back ", x", x represents a string starting from 0:

From this you can see, we can each get the date, month, year, equivalent in this way.

Similarly you can get time in this way.

 

Final Results

Here is an example:

@echo off
set d=%date:~0,2%
set mo=%date:~3,2%
set y=%date:~6,4%
set h=%time:~0,2%
set mi=%time:~3,2%
echo %y%_%mo%_%d%_%h%_%mi%
@echo on

The results obtained were as follows:

E:\Codes\bat>TestDate.bat
2019_10_27_20_39

 

He published 197 original articles · won praise 193 · Views 600,000 +

Guess you like

Origin blog.csdn.net/jiangwei0512/article/details/102770623
Recommended