0

If my system date is 25/09/2019 and time is 08.12 then i want output from batch file as 250920190812

But this code gives me 01 as output if i want to get day of todays date which is 25

echo Current day %date:~7,2%

What is wrong?

James
  • 1,637
  • 4
  • 35
  • 56
  • 2
    In your provided code snippet, `%date:~7,2%`, you're expanding the `%date%` value, `25/09/2019`, skipping the first `7` chartacters, `25/09/2`, then printing the next `2`, `01`. Therefore you'd want to skip `0` characters and print the next `2`, i.e. `%DATE:~0,2%`. To get a string similar to your emboldened example, you'd need to use the `%TIME%` variable too. – Compo Sep 25 '19 at 10:16
  • 1
    Take a look here: https://stackoverflow.com/a/19799236/388389 – npocmaka Sep 25 '19 at 12:19

1 Answers1

1

System Locale differs on each system, so rather use something more robust that will give consistant results on each device, for instance here is an example that also incorporates vbs.

@echo off
echo >"%temp%\%~n0.vbs" s=DateAdd("d",0,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "mydate=%%a"
set dd=%mydate:~6,2%
set mm=%mydate:~4,2%
set yyyy=%mydate:~0,4%
set mytime=%time::=%
set mytime=%mytime: =0%
echo %dd%%mm%%yyyy%%mytime:~0,4%
del "%temp%\%~n0.vbs" /Q
Gerhard
  • 18,114
  • 5
  • 20
  • 38
  • I asked one more question. see here https://stackoverflow.com/questions/58110705/date-and-time-not-saved-in-correct-format-while-executing-batch-file – James Sep 26 '19 at 06:26