1

How to change the date format of today's date in Command prompt I am trying to fetch the current date using %date% but i want to format the date as yyyy-mm-dd.

Joey
  • 316,376
  • 76
  • 642
  • 652
Anirudh
  • 63
  • 1
  • 6

2 Answers2

0

To get the formatting you want, you need to parse the output.

for /F "tokens=2-4 delims=/ " %i in ('date /t') do echo %k-%i-%j

Andrew
  • 678
  • 4
  • 8
  • I'd strongly advise against this, as the code will stop working when your regional settings change. – Joey Oct 29 '14 at 13:25
  • I've used it without a problem for about a two decades now, so I'm not too worried ;-) – Andrew Oct 29 '14 at 13:32
  • That's fine, but bear in mind that people finding this question might try building a batch file that has to run robustly on customer machines where you have no control over those things. Parsing output also can break down with certain locales that include non-ASCII letters in their date format. – Joey Oct 29 '14 at 14:03
0

If you are comfortable using date.exe (part of UnxUtils) there is a related answer here https://stackoverflow.com/a/1951681/422842

Community
  • 1
  • 1
f01
  • 1,370
  • 1
  • 14
  • 20