0

I have a batch file which creates a backup of some source code folders when run. For back file name it creates a string consisting of current date time.

To get date time string following snippet is used.

SET isodt=%date:~-4%_%date:~3,2%_%date:~0,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %isodt%
pause

It gives following output when run on my laptop:

2019_09_12__14_25_01

Recently I ran it on a virtual machine and it gave me following output for same code.

2019_ 0_Th__14_18_18

Why is output not the same? Both are run on Windows 10 64 bit machines.

Ram
  • 799
  • 15
  • 32
  • because system locale can differ between devices.. do `echo %date%` on both systems and see the results. – Gerhard Sep 12 '19 at 09:11
  • 2
    There are literally hundreds of examples here on [SO] on how to get date time in a locale/user settings independent format using WMIC/PowerShell/vbs. –  Sep 12 '19 at 09:16
  • For instance, if you use this on either system, you will get the same result each and every time you run it. `powershell get-date -format {yyyy_MM_dd_HH_mm_ss}` – Gerhard Sep 12 '19 at 09:18

1 Answers1

1

There are many methods of getting a none PC/locale dependent date and time, here's one:

@Echo Off
Set "isodt="
For /F "Tokens=1-6 Delims=/: " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined isodt Set "isodt=%%A_%%B_%%C__%%D_%%E_%%F"
If Not Defined isodt Exit /B
Compo
  • 30,301
  • 4
  • 20
  • 32