Format Date string method under three DOS batch processing

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's going to take a little bit of work.

It's annoying to talk about the output of a date string.     Under Linux, a simple word: Date ' +%y%m%d-%h%m%s ' can get the output you want:20120331-064219  but under Windows, it would take a bit of effort.     1. Format output directly with strings     For example, if the output of the time on your machine is such a:  c:>echo%date%-%time%  2012-03-31 Saturday-6:44:02.50  So, to get output 20120331-64402, you can intercept the string:  C:>echo%date:~0,4%%date:~5,2%%date:~8,2%-%time:~0,2%%time:~3,2%% time:~6,2%  20120331-65406  represents starting from 0 position 4 characters and so on. This method cannot truncate spaces. More annoyed     2. Splitting the intercept with a for statement seems better.     First look for the usage:    code is as follows: for/f ["Options"]%variable in (file-set) Do command [command-parameters] for/f [' Options ']%variable in (' String ') do command [command-parameters] for/f [option S "]%variable in (' Command ') do command [command-parameters]     optinos  eol=c          -refers to the end of a line comment character (just one)//to ignore lines beginning with what character   skip=n          -refers to the number of rows ignored at the beginning of a 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=;:. used ";", ":", "." Do separate                     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                     symbol string Last word asterisk number,                    So the additional variables will be after the last symbol parsing                   &NBS P Assign and accept the reserved text for the line.   USEBACKQ        -specifies that the new syntax is used in the next class case:                    Executes a string of post quotes as a command and a single                     quote character for text string command and allow in Filena meset                    use double quotes to expand the file name.   Refer to 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 ', so take 3Segment out, such as:  c:>for/f "Tokens=1-3 delims=-"%1 in ("%date%") do @echo%1%2%3  20120331  see time:  c:>e Cho%time%  6:59:20.38  c:>for/f "Tokens=1-3 delims=.:"%1 in ("%time%") do @echo%1%2%3  The two can be combined in this way: The   code is as follows: c:>for/f "Tokens=1-3 delims=-"%1 in ("%date%") do set ttt=%1%2%3  C:>set ttt=2012 0331  c:>for/f "Tokens=1-3 delims=.:"%1 in ("%time%") do set ttt=%ttt%-%1%2%3  C:>set 1     Write batch processing, it becomes like this (Goodtime.bat):      code as follows: for/f "Tokens=1-3 delims=-"%%1 in ("%date%") do SE T ttt=%%1%%2%%3 for/f "Tokens=1-3 delims=.:"%%1 in ("%time%") do set ttt=%ttt%-%%1%%2%%3     Echo goodtime=%t tt%  This method is more flexible.     3. Another is to use VBScript to customize the output       code as follows: WScript.Echo   Year (date)   &   Right ( "0"   &   Month (date), 2   &   Right ("0"   &   Day (date), 2)   &   "-"& Right ("0"   &   Hour (time), 2)   &   Right ("0"   &   Minute (time), 2) &nbsp ;     results are as follows, or more interesting     code is as follows: C:>cscript/nologo C:shareddatescript.vbs 20120331-0711 Of course, this is not ideal, Can write a batch processing, the results into the environment variables: Mydate2.bat, the following: @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 3rd method is the most complex and is not yet determined to 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.      

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.