0

In batch file, I query the date/time via WMIC, then reconfiguring it a "YYYYMMDD.HHMMSS" format...

@Echo Off
Cls
SetLocal EnableDelayedExpansion EnableExtensions
Call :DATETIME
Echo.
Echo  %DateTime%^|Info^|Process started...
REM DO STUFF
Ping -n 8 localhost>Nul 2>Nul
REM FINISH DOING STUFF
Call :DATETIME
Echo.
Echo  %DateTime%^|Info^|Process complete.
REM CALCULATE DIFFERENCE BETWEEN START AND FINISH DATETIME STAMPS HERE
Echo.
Echo  Run time was [days_and-or_hours_and-or_minutes_and-or_seconds].
EndLocal
Exit /B
:DATETIME
For /F "Tokens=* Delims=" %%A In ('WMIC OS Get LocalDateTime ^|Find "."') Do @Set DT=%%A
Set Year=%DT:~0,4%
Set Month=%DT:~4,2%
Set Day=%DT:~6,2%
Set Hour=%DT:~8,2%
Set Minute=%DT:~10,2%
Set Second=%DT:~12,2%
Set DateTime=%Year%%Month%%Day%.%Hour%%Minute%%Second%
Goto :EOF

How can I calculate the date/time difference (it may go over one day) between those two custom date/time stamps? It seems like it should be a simple task, but just can't figure it out.

Thanx in advance.

user3208239
  • 691
  • 1
  • 7
  • 15
  • This site helps you to fix your submitted code, this site is not here to add code to what you've submitted, in order to complete your stated task. Please use the search facility, at the top of the page, to help locate, and adapt something. If that code then fails to work as intended, you may have the ingredients for an on topic question. – Compo Mar 23 '23 at 18:22
  • Edited to include existing code. – user3208239 Mar 23 '23 at 19:06
  • There are already questions and answers which determine a time difference. You don't appear to have attempted it yourself. As I said, please attempt it by adapting existing code from a site search. – Compo Mar 23 '23 at 21:21
  • You can review [this answer](https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file/9935540#9935540) for a simple method to get the elapsed time in a Batch file... – Aacini Mar 24 '23 at 02:35
  • Did my updated answer do the trick for you? – SunsetQuest Apr 04 '23 at 20:40

2 Answers2

1

This should do the trick...

@Echo Off
Cls
SetLocal EnableDelayedExpansion EnableExtensions

For /F "Tokens=* Delims=" %%A In ('WMIC OS Get LocalDateTime ^|Find "."') Do @Set DT=%%A
Set Year0=%DT:~0,4%
Set Month0=%DT:~4,2%
Set Day0=%DT:~6,2%
Set Hour0=%DT:~8,2%
Set Minute0=%DT:~10,2%
Set Second0=%DT:~12,2%
Set DateTime0=%Year0%-%Month0%-%Day0%.%Hour0%:%Minute0%:%Second0%

Echo.
Echo  %DateTime0%^|Info^|Process started...
REM DO STUFF
Ping -n 8 localhost>Nul 2>Nul
REM FINISH DOING STUFF

For /F "Tokens=* Delims=" %%A In ('WMIC OS Get LocalDateTime ^|Find "."') Do @Set DT=%%A
Set Year1=%DT:~0,4%
Set Month1=%DT:~4,2%
Set Day1=%DT:~6,2%
Set Hour1=%DT:~8,2%
Set Minute1=%DT:~10,2%
Set Second1=%DT:~12,2%
Set DateTime1=%Year1%-%Month1%-%Day1%.%Hour1%:%Minute1%:%Second1%

Echo.
Echo  %DateTime1%^|Info^|Process complete.
REM CALCULATE DIFFERENCE BETWEEN START AND FINISH DATETIME STAMPS HERE
set /a "Seconds = (1%Day1% - 1%Day0%) * 86400 + (1%Hour1% - 1%Hour0%) * 3600 + (1%Minute1% - 1%Minute0%) * 60 + (1%Second1% - 1%Second0%)"

Echo.
Echo  Run time was %Seconds% seconds.
EndLocal
Exit /B

The output...

 2023-03-23.19:49:55|Info|Process started...

 2023-03-23.19:50:02|Info|Process complete.

 Run time was 7 seconds.
SunsetQuest
  • 8,041
  • 2
  • 47
  • 42
-1

I think batch files are outdated these days. I used to use them often and have switched to C#. Once you get over the bump of learning it, you can do these things easily: (vs. the batch file script is tough to work with!)

  1. download/install visual studio community (its free)
  2. create a new C# console application.
  3. enter the code below
  4. In the project file, you will see the exe file.

Hopefully this helps. I know this is not exactly what you were looking for.

using System;

DateTime startDateTime = DateTime.Now;
Console.WriteLine($"{startDateTime}|Info|Process started...");

/////// DO STUFF //////////
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "Ping.exe";
startInfo.Arguments = " -n 8 localhost";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
//////// END /////////////////

DateTime endDateTime = DateTime.Now;
Console.WriteLine("{endDateTime}|Info|Process complete.");

// calculate date/time difference 
TimeSpan runTime = endDateTime - startDateTime;
Console.WriteLine($"Run time was {FormatTimeSpan(runTime)}.");


static string FormatTimeSpan(TimeSpan timeSpan)
{
    return $"""
            {timeSpan.Days} day{(timeSpan.Days == 1 ? "" : "s")} 
            {timeSpan.Hours} hour{(timeSpan.Hours == 1 ? "" : "s")}
            {timeSpan.Minutes} minute{(timeSpan.Minutes == 1 ? "" : "s")}
            {timeSpan.Seconds} second{(timeSpan.Seconds == 1 ? "" : "s")}
            """;
}
SunsetQuest
  • 8,041
  • 2
  • 47
  • 42
  • 1
    Sadly, has to remain in batch file. Unable to utilize any third-party coding platforms (can't even use Powershell). – user3208239 Mar 23 '23 at 19:46