Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am tried to show the auto increment time between start datetime and end datetime. And it's working fine for only for same day where start datetime and end datetime. If start and end datetime is different it's not working correctly. For these I tried below code.

What I have tried:

JavaScript Code

JavaScript
<script type="text/javascript">
        function toSeconds(time_str) {
            var parts = time_str.split(':');
            return parts[0] * 3600 +
                parts[1] * 60 +
                +
                parts[2];
        }

        function StartTimer(startTimeP, endTimeP) {
            var difference = Math.abs(toSeconds(startTimeP) - toSeconds(endTimeP));
            //console.log(difference);
            var result = [
                Math.floor(difference / 3600),
                Math.floor((difference % 3600) / 60),
                difference % 60
            ];

            var x = setInterval(function () {
                result[2] = result[2] + 1;
                if (result[2] >= 60) {
                    result[2] = 0;
                    result[1] = result[1] + 1;
                }
                if (result[1] >= 60) {
                    result[1] = 0;
                    result[0] = result[0] + 1
                }

                result[0] = result[0] === 24 ? 0 : result[0]

                var temp = result.map(function (v) {
                    var tempResult = v < 10 ? '0' + v : v;
                    return tempResult;
                }).join(':');

                //console.log(temp);
                $(".worked_time").text(temp);
            }, 1000);
        }

        function Say() {
            //document.getElementById('AncClick').click();
            $("#AncClick").click();
        }
    </script>


And Label in Default.aspx file

ASP.NET
<asp:Label ID="lblTrackWorkedHours" runat="server" CssClass="worked_time" ToolTip="Worked Hours"></asp:Label>


Default.aspx.cs

C#
ScriptManager.RegisterStartupScript(Page, GetType(), "myscript", "StartTimer('11:34:25 PM','15:55:38 PM');", true);


Here 11:34:25 PM is 27-02-2019 11:34:25 PM and 15:55:38 PM is 28-02-2019 15:55:38 PM . So if I calculate result should be 16 Hours +. but it's showing 7+ Hours. It's calculating wrong.

So suggest us to display correctly with increment means after resulting it should be increment auto like timer.
Posted
Updated 28-Feb-19 2:12am
v2

The best way in any language to work with dates and time is to work with them in their respective DateTime format and not parsing it as a string.
While you are working with this the actual variables are just going to be numbers, and are much easier to work with as it is simple subtraction.

What I have done is defined your variables as Dates and then subtracted. As this is in milliseconds I divided by 1000 to get seconds. Once you have elapsed seconds you can easily convert to minutes and hours
JavaScript
var date1 = new Date('2019-02-27T23:34:25');
var date2 = new Date('2019-02-28T15:55:38');
var ElapsedSeconds = (date2 - date1) / 1000;
// var ElapsedHours = ElapsedSeconds / 3600;
 
Share this answer
 
Finally solved my self, writing below code.

JavaScript
<script type="text/javascript"> 
        function StartTimer(startDateTimeP, endDateTimeP) {

            var date1 = new Date(startDateTimeP);
            var date2 = new Date(endDateTimeP);
            var oneDay = 24 * 60 * 60; // hours*minutes*seconds
            var oneHour = 60 * 60; // minutes*seconds
            var oneMinute = 60; // 60 seconds
            var firstDate = date1.getTime(); // convert to milliseconds
            var secondDate = date2.getTime(); // convert to milliseconds
            var seconds = Math.round(Math.abs(firstDate - secondDate) / 1000); //calculate the diffrence in seconds
            // the difference object
            var difference = {
                "days": 0,
                "hours": 0,
                "minutes": 0,
                "seconds": 0,
            }
            //calculate all the days and substract it from the total
            while (seconds >= oneDay) {
                difference.days++;
                seconds -= oneDay;
            }
            //calculate all the remaining hours then substract it from the total
            while (seconds >= oneHour) {
                difference.hours++;
                seconds -= oneHour;
            }
            //calculate all the remaining minutes then substract it from the total 
            while (seconds >= oneMinute) {
                difference.minutes++;
                seconds -= oneMinute;
            }
            //the remaining seconds :
            difference.seconds = seconds;
            //return the difference object
            //$('#countdown').text(difference.days + ' Days ' + difference.hours + ' Hours ' + difference.minutes + ' Minutes ' + difference.seconds + ' Seconds');
            //console.log(difference);
            return difference;
        }
        setInterval(function () {
            //'02/27/2019 11:34:25 PM'
            var result = StartTimer(new Date('02/27/2019 11:34:25 PM'), new Date());
            $('#countdown').text(result.days + ' Days ' + result.hours + ' Hours ' + result.minutes + ' Minutes ' + result.seconds + ' Seconds');
            //StartTimer();
        }, 1000);



    </script>


Above solution is working as expected, But how to call this function inside the c#.

Please suggest me.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900