-2

to follow up on the last solution in this link https://www.windows-commandline.com/get-date-time-batch-file/

I know there's no windows CMD format with this but I've tried to batch replace and arrays apparently don't have good solutions if you Google for an answer since most of the time people find and replace things in files I keep getting those answers.

higuys
  • 17
  • 3

2 Answers2

0

If you are on a supported Windows system, powershell.exe will be available.

C:>@FOR /F "delims=" %A IN ('powershell.exe -NoLogo -NoProfile -Command Get-Date -Format 'M-d-yy'') DO @(SET "THEDATE=%~A")

C:>echo %THEDATE%
1-21-22

If this is in a .bat file script, double the PERCENT SIGN characters on the loop variable. %%A

lit
  • 14,456
  • 10
  • 65
  • 119
0

If you want a pure batch solution, wmic provides a few date-related strings without leading zeroes:

@echo off
setlocal
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do set "_%%x"
set "Mydate=%_Month%-%_Day%-%_Year%"
echo %mydate%
echo maybe also useful sometimes:
set _
Stephan
  • 53,940
  • 10
  • 58
  • 91