Link to home
Start Free TrialLog in
Avatar of econy1
econy1

asked on

Batch File - calculating difference (time lapsed) beween two sets of date/time

i have a batch file that will record the current date/time when the process begins, and then the date/time when it ends.  i will end up with 4 variables, 1 startDate, 1 startTime, 1 endDate, 1 endTime.  I want to calculate how much time has lapsed, so I will end up with an Hours value, a Minutes value, and a Seconds value.

I am currently obtaining the date/time like this:

::-----------------------------------------------------------
for /F "tokens=1,2" %%d in ('date /T') do set day=%%d & set date=%%e
set yyyy=%DATE:~6,4%
set dd=%DATE:~3,2%
set mm=%DATE:~0,2%
set startDate=%yyyy%-%mm%-%dd%

set startTime=%Time:~0,8%
::-----------------------------------------------------------

other processes (copying files, etc.)

::-----------------------------------------------------------
for /F "tokens=1,2" %%d in ('date /T') do set day=%%d & set date=%%e
set yyyy=%DATE:~6,4%
set dd=%DATE:~3,2%
set mm=%DATE:~0,2%
set endDate=%yyyy%-%mm%-%dd%

set endTime=%Time:~0,8%
::-----------------------------------------------------------


I would like to end up with 3 variables - lapsedHours, lapsedMinutes, lapsedSeconds.  I don't mind modifying the way in which I obtain the date or time, if necessary.





Avatar of 4auHuk
4auHuk
Flag of Russian Federation image

There's no built-in time/date arithmetics functionality in windows command shell as far as i know.
But you can use timethis.exe from Windows 2000 Resource Kit.

Download details: Windows 2000 Resource Kit Tool: Timethis.exe
http://www.microsoft.com/downloads/details.aspx?FamilyID=913795cd-7026-4143-ae85-1f5e096f9be0&displaylang=en
By "no built-in functionality" i mean that there's no easy native way to do date/time arithmetics (like DateDiff in VBScript).

Here's another topic near where SteveGTR expert proposes a batch script that does date arithmetics.
https://www.experts-exchange.com/questions/20898384/creating-batch-files.html

As you can see from this topic, it indeed can be done, but this will be a challenge as you need to implement all date arithmetics logic  in script by your self.

Best,
4auHuk
Avatar of econy1
econy1

ASKER

Thanks for the advice.  Timethis.exe probably won't work for my needs.  Something else I want to do is determine how many bytes per second the process ran.  Ultimately, in order to do this, I need to end up with a total count of minutes.  If I can get the  lapsedHours, lapsedMinutes, and lapsedSeconds variables, I can calculate the total seconds.  I also have the total bytes I am processing, so I will be able to calculate bytes/second.  I can do this piece myself, but do not believe it will be possible using timethis.exe.  

It's possible the other EE answer could be tweaked to do what I want, but I'm not an expert batch writer.  Any chance a script could be provided that will do exactly what I am looking for?
>Timethis.exe probably won't work for my needs.
Why's that?

>Any chance a script could be provided that will do exactly what I am looking for?
I was thinking that there *must* be some general solution for date arithmetics since this is wery useful functionality in administrative scripts. And here's what i found. This should be very close to what you need:

0721 » General purpose date math routine:
http://www.jsiinc.com/subb/tip0700/rh0721.htm

Be sure to read description thoroughly.

Best,
4auHuk
Avatar of econy1

ASKER

Timethis.exe appears to only produce an output in the command window.  I'll need values returned in variables, or something else that I can work with.

The tips on jsiinc.com are interesting.  However, i didn't find a single solution that would do everything I need (take a date AND a time and subtract another date AND time set).  In addition to the date subtraction tip you provided, I found a seperate time subtraction tip (tip# 0863).  I need something that will handle the case where I start the script on one day (11:55pm, for example) and it finishes the next day.

This may be a bit complicated, to do exactly what I need - I have increased the points.  

 
Okay, try this out. I've hardcoded the date and times for testing and I must admit I haven't fully tested it:

----- Start of Code

@echo off

REM set startDate=%date%
REM set startTime=%time%
set startDate=Sat 02/28/2004
set startTime=23:40:43.66

REM other processes (copying files, etc.)

REM set endDate=%date%
REM set endTime=%time%
set endDate=Sat 03/01/2004
set endTime= 0:47:45.66

echo startDate: %startDate% startTime: %startTime%
echo endDate:   %endDate% endTime:   %endTime%

set sdy=%startDate:~10%
set /a sdm=1%startDate:~4,2% - 100
set /a sdd=1%startDate:~7,2% - 100
set /a sth=%startTime:~0,2%
set /a stm=1%startTime:~3,2% - 100
set /a sts=1%startTime:~6,2% - 100

set edy=%endDate:~10%
set /a edm=1%endDate:~4,2% - 100
set /a edd=1%endDate:~7,2% - 100
set /a eth=%endTime:~0,2%
set /a etm=1%endTime:~3,2% - 100
set /a ets=1%endTime:~6,2% - 100

:CHECK_SEC

if /i %ets% LSS %sts% goto ROLL_MIN
set /a lts=%ets% - %sts%
goto CHECK_MIN

:ROLL_MIN
set /a stm+=1
set /a lts=%ets% + 60 - %sts%

:CHECK_MIN

if /i %etm% LSS %stm% goto ROLL_HOUR
set /a ltm=%etm% - %stm%
goto CHECK_HOUR

:ROLL_HOUR
set /a sth+=1
set /a ltm=%etm% + 60 - %stm%

:CHECK_HOUR
if /i %eth% LSS %sth% goto ROLL_DAY
set /a lth=%eth% - %sth%
goto CHECK_DAY

:ROLL_DAY
set /a sdd+=1
set /a lth=%eth% + 24 - %sth%

:CHECK_DAY
if /i %edd% LSS %sdd% goto ROLL_MONTH
set /a lth+=(%edd% - %sdd%) * 24
goto TALLY_MONTH

:ROLL_MONTH
call :GET_DAYS %sdm% %sdy%
set /a lth+=(%edd% + %dd% - %sdd%) * 24
set /a sdm+=1

:TALLY_MONTHS
if /i %sdm% EQU 13 (
  set /a sdm=1
  set /a sdy+=1
)
if /i %edm% EQU %sdm% goto TALLY_YEARS
call :GET_DAYS %sdm% %sdy%
set /a lth+=%dd% * 24
set /a sdm+=1
goto TALLY_MONTHS

:TALLY_YEARS
if /i %edy% EQU %sdy% goto DONE
set /a lth+=8760
call :GET_DAYS 2 %sdy%
if /i %dd% EQU 29 set /a lth+=24
set /a sdy+=1
goto TALLY_YEARS

:DONE

if /i %lts% GTR 59 (
  set lts%%=60
  set ltm+=1
)

if /i %ltm% GTR 59 (
  set ltm%%=60
  set lth+=1
)

echo Lapsed hours:   %lth%
echo Lapsed minutes: %ltm%
echo Lapsed seconds: %lts%

:CLEAN_UP

set tt=
set dd=

set lth=
set ltm=
set lts=

set sdy=
set sdm=
set sdd=
set sth=
set stm=
set sts=

set edy=
set edm=
set edd=
set eth=
set etm=
set ets=

set startDate=
set endDate=

goto EXIT

:GET_DAYS

if /i %1 EQU 1 goto SET31
if /i %1 EQU 2 goto LEAPCHK
if /i %1 EQU 3 goto SET31
if /i %1 EQU 4 goto SET30
if /i %1 EQU 5 goto SET31
if /i %1 EQU 6 goto SET30
if /i %1 EQU 7 goto SET31
if /i %1 EQU 8 goto SET31
if /i %1 EQU 9 goto SET30
if /i %1 EQU 10 goto SET31
if /i %1 EQU 11 goto SET30
if /i %1 EQU 12 goto SET31

:SET31

set dd=31

goto EXIT

:SET30

set dd=30

goto EXIT

:LEAPCHK

set /a tt=%2 %% 4

if /i %tt% NEQ 0 goto SET28

set /a tt=%2 %% 100

if /i %tt% NEQ 0 goto SET29

set /a tt=%2 %% 400

if /i %tt% EQU 0 goto SET29

:SET28

set dd=28

goto EXIT

:SET29

set dd=29

goto EXIT

:EXIT

----- End of Code

Good Luck,
Steve
SteveGTR,
That's great!

Only error i noticed is typo at line 65:
goto TALLY_MONTH
should be
goto TALLY_MONTHS

I also combined your script with UnivDate.bat  by JSI Inc. (http://www.jsiinc.com/subj/tip4800/rh4835.htm) to work with different date formats. I can post it here if you don't mind and anybody interested.

Good catch!
Here's the error correction:

:CHECK_DAY
if /i %edd% LSS %sdd% goto ROLL_MONTH
set /a lth+=(%edd% - %sdd%) * 24
goto TALLY_MONTHS

ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
econy1, is it working?
Avatar of econy1

ASKER

works great, thank you.