locked
How to retrive number of hour from two datetimes in lightswitch HTML Client? RRS feed

  • Question

  • Hi people,

    I have a table with two datetime values:

    StartTime and Endtime

    EndTIme its automatically filled, and i put manually starttime.

    I need to, when LS Html Client detects changes on Startime, puts result of diferent hours in a new field called total Hours worked.

    How it's possible?

    Saturday, October 11, 2014 7:20 PM

Answers

  • As the OP mentioned, a _Compute method won't be suitable since he (a) doesn't want this on the server, and (b) it's the HTML client, and server calculated values don't appear on the client.

    Dates are notoriously difficult in JavaScript, which is why some of the responses recommended using helper libraries such as moment.js.

    If. as you say, the end time is automatically assigned, and you want to enter the start time and figure out the difference, here's an example.

    Let's figure out the time left until Christmas from a particular date.  The LS screen will need local DateTime properties StartTime and EndTime, and I'll also use a local String property to display the result.

    First, assign the End Time to be Christmas Day:

    myapp.Christmas.created = function (screen) {
        // Write code here.
        screen.EndTime = new Date(2014, 11, 25, 0, 0, 0);
    };
    


    So there's weird JavaScript Date behavior already: months are zero-based so December = 11, while days are as-is.

    Next, add a method so that when anyone enters or changes the StartTime, the resulting time difference displays in the ChristmasTime local property.

    myapp.Christmas.ChristmasTime_postRender = function (element, contentItem) {
        // Write code here.
        var timeTilXmas = {};
    
        contentItem.dataBind('screen.StartTime', function (newValue) {
            if (newValue && newValue <= contentItem.screen.EndTime) {
                var timeSpan = contentItem.screen.EndTime - newValue;
                timeTilXmas = convertMS(timeSpan);
                contentItem.value = "There are " + timeTilXmas.d + " days, " + timeTilXmas.h + " hours, " + timeTilXmas.m + " minutes, and " + timeTilXmas.s + " seconds until Christmas.";
            }
        });
    
    };

    The variable timeSpan is the difference between the EndTime (Christmas) and the StartTime (user-entered), except that it's expressed in milliseconds.  A quick helper function convertMS() returns Days, Hours, Minutes, and Seconds.

    var convertMS = function (ms) { var d, h, m, s; s = Math.floor(ms / 1000); m = Math.floor(s / 60); s = s % 60; h = Math.floor(m / 60); m = m % 60; d = Math.floor(h / 24); h = h % 24; return { d: d, h: h, m: m, s: s }; }

    That's about as far as I can take the example, but at least you should be able to do the calculation you want to do with it.
    • Marked as answer by Ivan Martinez Wednesday, October 22, 2014 8:13 PM
    Wednesday, October 22, 2014 5:15 AM

All replies

  • Do you want to make the calculation before save (clienside) or after save (serverside)

    Sven Elm

    Sunday, October 12, 2014 7:39 AM
  • I need to see these field on screen. 

    After this field is entered, person verify that time is ok or no, and then they can manually change.

    Sunday, October 12, 2014 8:34 AM
  • We don't understand what you'r trying to do
    Friday, October 17, 2014 6:17 AM
  • Do you want to calculate between the StartTime and Endtime? Endtime-StartTime?
    Friday, October 17, 2014 6:18 AM
  • Hi ,

    i have a table like:

    Start Time

    End Time

    RealTimeInHours

    RealTimeInMinutes

    I need to put in fields RealTimeInHours / RealTimeInMinutes result of difference between the others fields. But NOT on server, i need to change if user needs RealTime Manually.

    Friday, October 17, 2014 6:50 AM
  • Friday, October 17, 2014 7:58 AM
  • HI Ivan,

    First it's recommended to use the link our community member(fileman) mentioned above to manipulate the StartTime and EndTime.

    If you want to calculate the StartTime and Endtime, you need calculated field to substract two datetime.

    <p>partialvoid ElapsedTime_Compute(refdouble result) { if (EndTime.HasValue && StartTime.HasValue) { result = ((TimeSpan)(EndTime - StartTime)).TotalMinutes; } }

    Please let me know if there is anything that I can do to help.

    Best regards,

    Angie


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Edited by Angie Xu Tuesday, October 21, 2014 7:16 AM
    Tuesday, October 21, 2014 7:15 AM
  • Hi Angie,

    your answer it's OK for a computed field (i give your answer in my favourite repository).

    I need, to retrieve when StartTime or EndTime values has changes and then put result in minutes in other field (not calculated).

    These field it's used to make an invoice, and it's succeptible to changes for administration person. For example:

    A technician makes a work for 5 hours, but, administration cannot make an invoice for 5 hours, make an invoice for 3 hours for example. They can change the value manually.

    Thanks!

    Tuesday, October 21, 2014 6:23 PM
  • So Ivan - as I read the restated requirement, you need a solution that will:

    • Automatically calculate the time elapsed between start time and end time, and push that across to elapsed hours and minutes controls.
    • Allow the elapsed hours and minutes controls to be over-ridden if needed before posting back to the server to allow users to make some kind of adjustment.

    In my experience, the best way to handle this scenario is calculate the actuals as per Angie's advice, but add an time adjustment field (or fields) which defaults to 0/null, but can be over-ridden.  When the invoice is generated, the chargeable hours are calculated subtracting the adjustment (if it exists) from the elapsed time.  This allows the accountants to track the amount of time being written down as well as generating accurate invoices.  In turn, that knowledge can be used to fine-tune the promises being made by the organization and to provide appropriate training/coaching to staff who need it.

    Hope that helps. :)

    Tuesday, October 21, 2014 11:14 PM
  • As the OP mentioned, a _Compute method won't be suitable since he (a) doesn't want this on the server, and (b) it's the HTML client, and server calculated values don't appear on the client.

    Dates are notoriously difficult in JavaScript, which is why some of the responses recommended using helper libraries such as moment.js.

    If. as you say, the end time is automatically assigned, and you want to enter the start time and figure out the difference, here's an example.

    Let's figure out the time left until Christmas from a particular date.  The LS screen will need local DateTime properties StartTime and EndTime, and I'll also use a local String property to display the result.

    First, assign the End Time to be Christmas Day:

    myapp.Christmas.created = function (screen) {
        // Write code here.
        screen.EndTime = new Date(2014, 11, 25, 0, 0, 0);
    };
    


    So there's weird JavaScript Date behavior already: months are zero-based so December = 11, while days are as-is.

    Next, add a method so that when anyone enters or changes the StartTime, the resulting time difference displays in the ChristmasTime local property.

    myapp.Christmas.ChristmasTime_postRender = function (element, contentItem) {
        // Write code here.
        var timeTilXmas = {};
    
        contentItem.dataBind('screen.StartTime', function (newValue) {
            if (newValue && newValue <= contentItem.screen.EndTime) {
                var timeSpan = contentItem.screen.EndTime - newValue;
                timeTilXmas = convertMS(timeSpan);
                contentItem.value = "There are " + timeTilXmas.d + " days, " + timeTilXmas.h + " hours, " + timeTilXmas.m + " minutes, and " + timeTilXmas.s + " seconds until Christmas.";
            }
        });
    
    };

    The variable timeSpan is the difference between the EndTime (Christmas) and the StartTime (user-entered), except that it's expressed in milliseconds.  A quick helper function convertMS() returns Days, Hours, Minutes, and Seconds.

    var convertMS = function (ms) { var d, h, m, s; s = Math.floor(ms / 1000); m = Math.floor(s / 60); s = s % 60; h = Math.floor(m / 60); m = m % 60; d = Math.floor(h / 24); h = h % 24; return { d: d, h: h, m: m, s: s }; }

    That's about as far as I can take the example, but at least you should be able to do the calculation you want to do with it.
    • Marked as answer by Ivan Martinez Wednesday, October 22, 2014 8:13 PM
    Wednesday, October 22, 2014 5:15 AM