-4
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b)

Here mydate is 20180416. How to get the date in yymmdd format here?

Lalatendu Bag
  • 11
  • 1
  • 3
  • you should better use a [solution independent of locale settings](https://stackoverflow.com/a/18024049/2152082). If you want to keep your current approach, just `set mydate=%mydate:~2%` to snip the first two characters. – Stephan Apr 16 '18 at 14:28
  • You were already shown how to do substrings and get the date independent of localization on [DosTip.com](https://www.dostips.com/forum/viewtopic.php?f=3&t=8326&p=55685#p55682) – Squashman Apr 16 '18 at 14:55

1 Answers1

0

For your specific locale dependent method, you just need to add an additional line removing the first two characters:

@Echo Off
For /F "Tokens=2-4 Delims=/ " %%A In ('Date /T') Do Set "mydate=%%C%%A%%B"
Set "mydate=%mydate:~2%"
Echo [%mydate%]
Timeout -1

Although for that format you could probably do it even simpler:

@Echo Off
Set mydate="%DATE:~-2%%DATE:~-10,2%%DATE:~-7,2%"
Echo [%mydate%]
Timeout -1

Alternatively for a locale independent method…

You could use RoboCopy:

@Echo Off
For /F "Tokens=1-3 Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do Set "mydate=%%A%%B%%C" & GoTo :Break
:Break
Set "mydate=%mydate:~2%"
Echo [%mydate%]
Timeout -1

WMIC:

@Echo Off
For /F "EOL=L" %%A In ('WMIC OS Get LocalDateTime'
) Do Set "mydate=%%~nA" & GoTo :Break
:Break
Set "mydate=%mydate:~2,6%"
Echo [%mydate%]
Timeout -1

MakeCab:

@Echo Off
Set "df=%TEMP%\~foo.ddf"&Set "tf=%TEMP%\~%RANDOM%"
Set /A "Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12"
(   Echo .Set InfHeader=""&Echo .Set InfSectionOrder=""&Echo .Set InfFooter="%%2"
    For /L %%A In (1 1 4) Do Echo .Set InfFooter%%A=""
    Echo .Set Cabinet="OFF"&Echo .Set Compress="OFF"&Echo .Set DoNotCopyFiles="ON"
    Echo .Set RptFileName="NUL")>"%df%"
MakeCab /D InfFileName="%tf%" /F "%df%">Nul
For /F "UseBackQ Tokens=2-3,5" %%A In ("%tf%") Do Set /A "mm=%%A,dd=10%%B,yyyy=%%C"
Del "%tf%" "%df%">Nul 2>&1&Set "mm=10%mm%"
Set "mydate=%yyyy:~-2%%mm:~-2%%dd:~2%"
Echo [%mydate%]
Timeout -1

Or even utilise PowerShell:

@Echo Off
For /F %%A In ('PowerShell "(Get-Date).ToString('yyMMdd')"') Do Set "mydate=%%A"
Echo [%mydate%]
Timeout -1
Compo
  • 36,585
  • 5
  • 27
  • 39