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
Batch variable containing date mmddyyyy to convert to yymmdd
Moderator: DosItHelp
Re: Batch variable containing date mmddyyyy to convert to yy
Solved, nevermind.
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Batch variable containing date mmddyyyy to convert to yy
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):
With enabledelayedexpansion:
Of course, if you need this in a variable, just change:
echo:----------/%%y/%%x
to
set "variable=----------/%%y/%%x"
in either code.
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
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)
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)