-1

I have a date i.e. 08/27/2016 wanted to pass this date to cmd and it should return me the date as 201627. How can I do this?

user692942
  • 16,398
  • 7
  • 76
  • 175
Anoop
  • 439
  • 2
  • 7
  • 12
  • Really? What have you tried? - `Year(yourdate) & Right("00" & Day(yourdate), 2)` will do it. Take a look at [Format current date and time](http://stackoverflow.com/questions/22574092/format-current-date-and-time/22575530#22575530) describes some of these functions and techniques in detail. – user692942 Aug 31 '16 at 12:50

1 Answers1

1

In CMD you'd use environment variable substitution:

set "d=08/27/2016"

set "month=%d:~0,2%"
set "day=%d:~3,2%"
set "year=%d:~6,4%"

echo %year%%day%

Unfortunately Microsoft seems to have removed that from the official documentation, so I'm quoting the relevant passage from set /?:

Environment variable substitution has been enhanced as follows:

%PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1.

May also specify substrings for an expansion.

%PATH:~10,5%

would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified.

%PATH:~-10%

would extract the last 10 characters of the PATH variable.

%PATH:~0,-2%

would extract all but the last 2 characters of the PATH variable.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Still answering question like this, just breeds more like it. No clear problem, requesting multiple methods *(VBScript, Cmd etc.)*. They are poor quality and shouldn't be encouraged. – user692942 Aug 31 '16 at 16:49
  • The [tag:cmd] and [tag:batch] tags are less strict in this regard than other tags. – Ansgar Wiechers Aug 31 '16 at 21:25