DOS datetime formatting use summary (date and time) _dos/bat

Source: Internet
Author: User
Copy code code as follows:

@echo off
@title the edit test for the string (described below with the arrow →← meaning right and left respectively)
Set aa=1234567890
Echo Description: The following compound variable, the number before the comma indicates the pointer offset, and the number after the comma indicates the length of the extracted character
echo%aa:~1,5%//pointer to right → Offset 1-bit, and then start at the pointer to the right → extract 5 characters.
The echo%aa:~0,-3%//pointer is not offset, → extracts all characters, and removes the last 3 characters.
echo%aa:~5%//single digit, no length value specified. Represents a pointer → offset 5 characters, and then → extracts all characters.
echo%aa:~-5%//single negative, special case, indicating the reverse direction of the pointer offset, from the right end, ← Offset 5 bits, and then start from the pointer to extract all the characters. It can also be simply understood as extracting 5 characters from the end of a string.

The case where the echo pointer is offset in the opposite direction. Both from right to left offset from the right end of the string. ←

The Echo%aa:~-2,1%//Offset is negative, indicates that the pointer is from the right end, ← to the left to offset 2 digits, and then from the pointer to the right → extract 1 characters, to generate a new variable value. Note The space at the end of the string, because the space is also extracted as a "transparent character" into the variable, Remember to remove the space at the end of the variable. Ctrl + a quickly and easily check whether the end of the variable string contains spaces.
echo%aa:~-4,3%//The right end, the pointer ← Offset 4 bits, and then → extract 3 characters.
echo%aa:~-4,-2%//From the right end, the pointer ← Offset 4 bits, and then start at the pointer, → Extract all the characters, and remove the last 2 characters.

echo Application
echo%date:~0,10%//Extract Date
echo%date:~-3%//Extract Week information
echo%time:~0,5%//extraction time and minutes
echo%time:~0,-3%//extract time and minute and second information
Pause

Summary:
1, when the offset is positive, indicating that the pointer moves to the right, or negative, that the pointer moves from the right end of the string to the left.
2. If the length value is positive, a number of characters are extracted to the right, and if negative, all characters are extracted to the right, and several characters are removed. The absolute value of a number of = length values. The direction of the extract character always goes from left to right.
3, if it is a single digit, then this number represents an offset, the length value is the default value, which is the "remainder" of the string, which is all the characters that start at the pointer and to the right. If this number is negative, it can be simply understood as: Extract several characters at the end of the string. (the correct explanation should be: Indicates that the pointer is offset in reverse direction, from the right end, ← offset several bits, and then start at the pointer → Extract all characters
%date% and%time% are date variables and time variables built into the system.

Set date=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%
%date:~0,4% says years, and so on
The format displayed is: 20120515105610

DOS date format settings

%date% and%time%
2008-10-21 21:34:11.56
C:\>echo%date%
2008-10-21 Tuesday

C:\>echo%date:~0,4%
2008

Letter:\> Output%date% is a parameter but the one behind him is the range of values, and the first 0 represents a total of 4 characters after the beginning of the 4.

C:\>echo%date:~-3%
Wednesday
The negative numbers represent the reciprocal, and the last 3 characters are taken.
C:\>echo%date:~-3,-1%
Week
Here's-1 is not outputting the last 1 characters-2 of course 2 analogies

Time is similar

Echo%time%

21:34:11.56

DOS command date and time

Data Date setting command

Function: Sets or displays the system date.
Type: Internal command
Format: Date[mm-dd-yy]
Instructions for use:
(1) Omit [mm-dd-yy] Display system date and prompt to enter new date, do not modify can press ENTER directly, [MM-DD-YY] for "Month month-day-year" format;
(2) When the machine starts, there is an automatic processing file (Autoexec.bat) is executed, the system is not prompted to enter the system date. Otherwise, you are prompted to enter a new date and time.
Instance:
View the date of the current system and enter date directly.
  
second, time system clock Setup command

Function: Sets or displays the system period.
Type: Internal command
Format: time[hh:mm:ss:xx]
Instructions for use:
(1) Omit [hh:mm:ss:xx], display system time and prompt to enter new time, do not modify can press ENTER directly, [HH:MM:SS:XX] is "hour: minute: Second: percent of seconds" format;
(2) When the machine starts, there is an automatic processing file (Autoexec.bat) is executed, the system is not prompted to enter the system date. Otherwise, you are prompted to enter a new date and time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.