Batch variable containing date mmddyyyy to convert to yymmdd

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Deana
Posts: 2
Joined: 15 Sep 2010 08:32

Batch variable containing date mmddyyyy to convert to yymmdd

#1 Post by Deana » 15 Sep 2010 08:35

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Batch variable containing date mmddyyyy to convert to yy

#2 Post by !k » 15 Sep 2010 08:55

Read set/?

Deana
Posts: 2
Joined: 15 Sep 2010 08:32

Re: Batch variable containing date mmddyyyy to convert to yy

#3 Post by Deana » 15 Sep 2010 10:01

Solved, nevermind.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Batch variable containing date mmddyyyy to convert to yy

#4 Post by orange_batch » 15 Sep 2010 14:54

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.

Spellman
Posts: 5
Joined: 15 Sep 2010 03:50

Re: Batch variable containing date mmddyyyy to convert to yy

#5 Post by Spellman » 15 Sep 2010 19:16

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)

Post Reply