1

I have two date time like following:-

Start Time:- 11/27/2017 11:18:14 PM

End Time :- 11/28/2017 4:18:22 AM

How to get the difference between them in the hours:minute:second format using batch script? Please help me...

Community
  • 1
  • 1
  • 4
    Possible duplicate of [Calculate time difference in Windows batch file](https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file) – Hille Nov 28 '17 at 14:05
  • @Hille It is not duplicate of that question...it includes both date and time – user9020396 Nov 28 '17 at 14:20
  • Where do you have those dates and times? are they read from file content? or are they attained from output? and if so how? _(please show us)_. – Compo Nov 28 '17 at 14:26
  • They are stored in a file – user9020396 Nov 28 '17 at 14:29
  • 1
    When I said "If so how? _(please show us)._", the intention was to determine how you were initially outputting those dates and times to the file. If they are being determined as the result of a running script or process then it may be better advised to parse and calculate the duration pre file output where you could have better control of the format. A post process calculation is restricted to working directly with non specific localised text strings. – Compo Nov 28 '17 at 15:24

2 Answers2

0

Try this (I called it datediff.bat):

@if (@X)==(@Y) @end /* JScript comment
    @echo off

    cscript //E:JScript //nologo "%~f0" %*

    exit /b %errorlevel%


@if (@X)==(@Y) @end JScript comment */

//WScript.Echo(WScript.Arguments.Item(0));

var date1=WScript.Arguments.Item(0);
var date2=WScript.Arguments.Item(1);

function fromStringToDate(dateString) {
    var cd=dateString.split(" ")[0];
    var hm=dateString.split(" ")[1];
    var pm=dateString.split(" ")[2].toLowerCase;

    var mon=parseInt(cd.split("/")[0]);
    var day=parseInt(cd.split("/")[1]);
    var yea=parseInt(cd.split("/")[2]);

    var hou=parseInt(hm.split(":")[0]);
    if(pm=="pm"){
        hou=hou+12;
    }
    var min=parseInt(hm.split(":")[1]);
    var sec=parseInt(hm.split(":")[2]);
    //WScript.Echo(hou+" "+mon);
    //WScript.Echo(yea+","+mon+","+day+","+hou+","+min+","+sec);
    date= new Date(yea,mon,day,hou,min,sec,0);
    //WScript.Echo(date);
    return date;
}

var d1=fromStringToDate(date1);
var d2=fromStringToDate(date2);
var timeDiff = Math.abs(d1.getTime() - d2.getTime());

var diffDays = Math.floor(timeDiff / (1000 * 3600 * 24)); 
timeDiff =(timeDiff-diffDays*1000 * 3600 * 24);

var diffHours= Math.floor(timeDiff / (1000 * 3600));
timeDiff =(timeDiff-diffHours*1000 * 3600);

var diffMinutes= Math.floor(timeDiff / (1000 * 60));
timeDiff =(timeDiff-diffMinutes*1000 * 60);

var diffSeconds= Math.floor(timeDiff / (1000));

WScript.Echo("days: " +diffDays);
WScript.Echo("hours: " +diffHours);
WScript.Echo("minutes: " +diffMinutes);
WScript.Echo("seconds: " +diffSeconds);

And tested it like:

datediff.bat "11/27/2017 11:18:14 PM" "11/28/2017 4:18:22 AM"

It will work only in the format you've posted.

npocmaka
  • 51,748
  • 17
  • 123
  • 166
0

In this PowerShell script wrapped in a batch the hours will be totalhours,
each day difference counts 24h

:: Q:\Test\2017\11\28\SO_47532900.cmd
@Echo off
Set "Start=11/26/2017 11:18:14 PM"
Set "End=11/28/2017 4:18:22 AM"
For /F "usebackqdelims=" %%A in (`
powershell -NoP -C "$TS=New-TimeSpan -Start ([datetime]'%Start%') -End ([datetime]'%End%');'{0}:{1:00}:{2:00}' -f [math]::Floor($TS.TotalHours),$TS.Minutes,$TS.Seconds"
`) Do Set "DTdiffH=%%A"
Set DTdiffH

Sample output:

> SO_47532900.cmd
DTdiffH=29:00:08