Page 1 of 1

Batch variable containing date mmddyyyy to convert to yymmdd

Posted: 15 Sep 2010 08:35
by Deana
Hello!

I have a long batch file that calculates a specific date based on user input, however, the variable echoed at the end contains mmddyyyy as format. This is NOT the sysdate. I need to convert this into yymmdd format and echo it. Variable gets the date by echo %mm%/%dd%/%yyyy% but of course changing the order and doing %yy%/%mm/%dd% still gives the year as 2010 and I only need 10. Is there a way to do this?

Thanks in advance

Dea

Re: Batch variable containing date mmddyyyy to convert to yy

Posted: 15 Sep 2010 08:55
by !k
Read set/?

Re: Batch variable containing date mmddyyyy to convert to yy

Posted: 15 Sep 2010 10:01
by Deana
Solved, nevermind.

Re: Batch variable containing date mmddyyyy to convert to yy

Posted: 15 Sep 2010 14:54
by orange_batch
On my system, %date% gives dd/mm/yyyy. Either way, this will reverse the date and truncate the last token to two characters (in this case, year).

Without enabledelayedexpansion ("last" variable cannot start with x, y or z if renamed):

Code: Select all

for /f "delims=/ tokens=1,2,3" %%x in ("%date%") do set "last=%%z"&call echo:%%last:~-2%%/%%y/%%x


With enabledelayedexpansion:

Code: Select all

setlocal enabledelayedexpansion
for /f "delims=/ tokens=1,2,3" %%x in ("%date%") do set "last=%%z"&echo:!last:~-2!/%%y/%%x


Of course, if you need this in a variable, just change:

echo:----------/%%y/%%x

to

set "variable=----------/%%y/%%x"

in either code.

Re: Batch variable containing date mmddyyyy to convert to yy

Posted: 15 Sep 2010 19:16
by Spellman
While this may be solved, I thought I'd put my input in as well.

set mm=%date:~4,2%
set dd=%date:~7,2%
set yy=%date:~12,2%

Then you have 2 options. If you want to use the variable often, then do:
set date_=%mm%%dd%%yy%%
(Obviously you can arrange them how you want)

Or you can just echo it
echo %mm%%dd%%yy%
(Again, arrange how you want)