A further discussion on the various methods of formatting date strings under DOS batch processing (detailed) _dos/bat

Source: Internet
Author: User
Under Linux, a simple word: Date ' +%y%m%d-%h%m%s ' can get the output you want: 20120331-064219
But under Windows, it will take a bit of effort to get this.

1. Format output directly with strings
For example, if the output of time on your machine is like this:
C:\>echo%date%-%time%
2012-03-31 Saturday-6:44:02.50
So, to get 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 www.jb51.net
Represents the start of 4 characters from the 0 position, and so on. This method cannot truncate spaces. More annoyed

2. Splitting the intercept with a for statement seems better.
Let's take a look at the for use:
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)//to ignore a line beginning with what character
Skip=n-refers to the number of rows ignored at the start of the file.
Delims=xxx-refers to the delimiter set. This replaces the space and the Jump bar//Specifies the split character to be included in the delims=;: Using the ";", ":", "." Do separate
The default delimiter set.
Tokens=x,y,m-n-refers to which symbol of each line is passed to each iteration
For itself. This results in an assignment of the extra variable names. M-n
Format is a range. Specify MTH by nth symbol. If
The last character in the symbol string, asterisk,
Then the extra variables will be followed by the last symbol resolution
Assign and accept the reserved text for the line.
USEBACKQ-Specifies that the new syntax is already used in the following class cases:
Executes a string of post quotes as a command and a single
The quote character is a literal string command and is allowed in the FileNameSet
Use double quotes to expand the file name.
By referencing this usage, we can format the date string output:
Because the date result is: 2012-03-31 Saturday
Because the separator character is '-' and a space ', take 3 paragraphs out, such as:
c:\>for/f ' Tokens=1-3 delims=-'%1 in ('%date% ') do @echo%1%2%3
20120331 www.jb51.net
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
A combination of the two can be used in this way:

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 in batches, 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. There is also the use of 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, or more interesting.
C:\>cscript/nologo C:\shared\datescript.vbs
20120331-0711
Of course, this is still not ideal, you can write a batch processing, the results into the environment variables:
Mydate2.bat, the contents are as follows:
@echo off
Cscript/nologo Datescript.vbs >> Tmp.txt
for/f "tokens=*"%%1 in (tmp.txt) do set goodtime=%%1
Echo goodtime=%goodtime%
To sum up, the 3rd approach is the most complex, and it is not yet certain whether it will work correctly on all Windows platforms, but the results should be fixed. Does not change as the date,time format changes. However, 1 and 2 will depend on the format of the date time. That's all.
Or Linux is more fixed.

This paper comes from the Iihero laboratory

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.