0

When you simply use %DATE% or %TIME% in batch file/ command prompt, it will give you a string that matches the system's short format. However, some command, such as robocopy, will display date and time that matchs the system's long format. Is there any simple way to get date/time string in system's long format? Thanks.

An example

For more detail, I am writing a batch file that will contain both hand-written date and time output, and output from command robocopy. I want them to be coherent for aesthetic reasons. So, let robocopy use the short format as date/time output is also an acceptable solution for me. I saw there are many answers on internet that tells how to convert one format to another. However, different computers have different format settings. So I do not perfer mannually convert but just pull string directly from system.

Intuitively, I tried %LONGDATE%, but it does not work without any suprise.

Alkaid C
  • 3
  • 1
  • Can you choose to use PowerShell instead of batch? You'd then get access to [.NET Standard date and time formatting](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings) – Damien_The_Unbeliever Apr 20 '23 at 06:25
  • When I read the title, I was really hoping that you were asking for ISO 8601 format so that you could use `wmic os get localdatetime`. Unfortunately, I'm not aware of anything built in in batch. – SomethingDark Apr 20 '23 at 06:35

1 Answers1

1

Easy.

@ECHO OFF
SETLOCAL

FOR /f "tokens=1*delims=: " %%b IN ('robocopy /?') DO IF "%%b"=="Started" SET "longdatetime=%%c"
SET longdatetime

GOTO :EOF
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 2
    `for /f "tokens=1* delims= " %%a in ('robocopy /? ^|findstr /brc:" [^^ /]" ') do @echo %%b` to make it language independent. – Stephan Apr 20 '23 at 07:47