Date field difference calculation

Hi all,
I am new to Adobe and Java scripting so apologies if this has already been answered elsewhere - I have not been able to find it if it has.
I want to calculate the difference between two dates fields in hours and mins on a form.
I have two fields, both Date format (dd/mm/yyy HH:MM), a Start and End date and I want the difference between them in the Time format HH:MM.
Can anyone help me with the script for this? What I have so far is:
var strStart = this.getField("StartTime").value;
var strEnd = this.getField("EndTime").value;
if(strStart.length || strEnd.length)
var dateStart = util.scand("dd/mm/yyyy HH:MM",strStart);
var dateEnd = util.scand("dd/mm/yyyy HH:MM",strEnd);
var diff = dateEnd.getTime() - dateStart.getTime();
// One Day = (24 hours) x (60 minutes/hour) x
// (60 seconds/minute) x (1000 milliseconds/second)
var oneMin = 60 * 60 * 1000;
var mins = Math.floor(diff/oneMin);
event.value = util.printd("HH:MM",mins);
But this is not working...
Mary

The result can be formatted using:
// format result using "h:MM" format
event.value = util.printf("%,0 0.0f" + ":" + "%,002.0f", nHours, nMinutes);
One cannot use the date or time formats since the time values will be limited to the hours and minutes values for 1 day, so any time value that is over 23 hours 59 minutes is not possible.
A full script solution including document level functions for conversion of date strings to minutes and converting minutes to a time string:
// reusable document level functions;
function Time2Num(cFormat, cDate) {
// convert date value with format to number of minutes form Epoch date;
var oDate = util.scand(cFormat, cDate);
var nMins =  null;
if(oDate ==  null) app.alert("Error converting " + cString + " using " + cForamt);
else nMins = oDate.getTime() / (1000 * 60);
return Math.floor(nMins);
} // end Time2Num format
function Num2Time(cFormat, nMins) {
// convert number of muniutes to h:MM or HH:MM format;
// return formatted string for valid formats;
// return null for invalid formats;
var cElapsed = null;
// test for nMins being a number;
if(isNaN(nMins)) {
app.alert("Minutes must be number",0, 0);
} else {
var nHours = Math.floor(nMins / 60);
var nMinutes = Math.floor(nMins % 60);
switch(cFormat) {
case "h:MM":
cElapsed = util.printf("%,0 0.0f" + ":" + "%,002.0f", nHours, nMinutes);
break;
case "HH:MM":
cElapsed = util.printf("%,002.0f" + ":" + "%,002.0f", nHours, nMinutes);
break;
default:
app.alert("Invalid format " + cFormat + "\nMust be \"HH:MM\" or \"h:MM", 0, 0);
break;
return cElapsed;
} // end Num2Time function
// end document level funcitons;
// custom calculation script;
event.value = ''; // clear result;
var strStart = this.getField("StartTime").value;
var strEnd = this.getField("EndTime").value;
if(strStart.length || strEnd.length) {
var nDateStart = Time2Num("dd/mm/yyyy HH:MM",strStart);
var nDateEnd = Time2Num("dd/mm/yyyy HH:MM",strEnd);
var nMins = nDateEnd - nDateStart;
// format result using "h:MM" format
event.value = Num2Time("h:MM", nMins);
// end custom calculation script;

Similar Messages

  • Add Days to a Date field

    Hi all,
    I have the following requirement:
    I have a date field 0calday and the user would like to add a specific no.of days to it, like for example 10 or 15 days. Then we use this computed date field in calculations in the query. Could you please help and let me know how we can no.of days to a date field? And also how we can compute no.of days between two date fields?
    Regards,
    Ashmith Roy

    Hi Ashmith,
        When the user enters both date and no. of days, and if you want to calculate no of days from the user entered date I think you can go for an exit where you can use the available function module which calculates the no of days from starting of the month for the given date. Then you can add up the days entered by the user and use another available function module which converts it to the dat format again based on the given days and the old date. I don't remember the function module names but i am sur ethey are available.
    If the above thing is made possible then I think getting no of days between two dates is not a problem. Well i hope this will help you.

  • Date Difference Calculation in BW3.5

    Hi
    We are calculating date differences between 2 dates i.e. Delivery Date - System Date.For System date we used variable exit to derive the value.But when we create CKF on Delivery Date,we necessary have to keep date field in Drilldown(in rows).But reports needs to give Overview while including date field in rows removes the granularity.
    Is thier any other way we can handle this situation.
    Regards
    Praveen

    You have to put ur delivery date in Rows if u r doing the Formula variable with replacement path. There is no option.
    Khaja

  • Update non-editable field Start date which is calculated from other context

    I had created some custom date fields(model, bol entity) in component ICCMP_BTSHEAD. We are calculating Start date based on the new input value into receiving date in the other context node.
    so, for this i get the value on controller class(_IMPL). and pass the same value in the GET method of Start date.
    But, Problem is Start date is the non-editable field so SET method will not be trigger as expected. so the same i used the DO_PREPARE_OUTPUT method but it's called and update the value when we press the enter. IF directly we click on the "SAVE" button then it' trigger but value is not updating in the database.
    Please, find the below piece of code for the same. can u please anybody help me that how can i achieve this requirement.
    DATA: l_recieve_date     TYPE crmt_date_timestamp_from,
            lr_current TYPE REF TO cl_bsp_wd_mixed_node,
            lr_col      TYPE REF TO cl_bsp_wd_collection_wrapper.
      DATA: lv_date TYPE d,
             lv_time TYPE t,
             lv_date_temp TYPE sydatum.
      IF iv_first_time EQ abap_false. "avoid for first tile load
    Read the value of btreceivedate-TIMESTAMP_FROM.
        lr_col = me->ztyped_context->btreceivedate->get_collection_wrapper( ).
        IF lr_col IS BOUND.
          lr_current ?= lr_col->get_current( ).
          IF lr_current IS BOUND.
            lr_current->if_bol_bo_property_access~get_property_as_value( EXPORTING iv_attr_name = 'TIMESTAMP_FROM'
                                       IMPORTING ev_result   = l_recieve_date ).
          ENDIF.
        ENDIF.
        IF l_recieve_date IS NOT INITIAL.
          CONVERT TIME STAMP l_recieve_date TIME ZONE sy-zonlo
                    INTO DATE lv_date TIME lv_time.
    Check day is working day - if not, correct to previous working day
          IF lv_date IS NOT INITIAL.
            lv_date_temp = lv_date.
            CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE'
              EXPORTING
                correct_option      = '-'
                date                = lv_date
                factory_calendar_id = 'GB'
              IMPORTING
                date                = lv_date_temp
              EXCEPTIONS
                date_after_range    = 1
                date_before_range   = 2.
            IF sy-subrc IS INITIAL.
              lv_date = lv_date_temp.
            ENDIF.
          ENDIF.
          CONVERT DATE lv_date TIME lv_time
                 INTO TIME STAMP l_recieve_date TIME ZONE sy-zonlo.
        ENDIF.
        lr_col = me->ztyped_context->btstart->get_collection_wrapper( ).
        IF lr_col IS BOUND.
          lr_current ?= lr_col->get_current( ).
          IF lr_current IS BOUND.
            lr_current->if_bol_bo_property_access~set_property(
                       iv_attr_name = 'APPT_TYPE'               "#EC NOTEXT
                       iv_value     = 'ZSTWTIME0001' ).
            lr_current->if_bol_bo_property_access~set_property(
                            iv_attr_name = 'TIMESTAMP_FROM'     "#EC NOTEXT
                            iv_value     = l_recieve_date ).
          ENDIF.
        ENDIF.
      ENDIF.
    could you please help me?

    Hi,
    Thanks for your reply,
    yes, i checked the same thing also. But DO_HANDLE_DATA is called before the SET method of receiving date and in that case we are getting the old value in DO_HANDLE _DATA and  source and Target fields both are not updating.
    I checked, if, i'll change the input field then set method of input field and then EH_ON_SAVE is called directly.
    i think, if i'll do the same modification in the EH_ON_SAVE method as i did in the DO_PREPARE_OUTPUT then it can be updated but i don't want to change the EH_ON_SAVE at this moment.
    is there any other way to update the START DATE(Display mode in the screen) which is calculate from the other context node field receiving date.

  • Group by on date field with time difference of up to 1 minute

    Hi,
    I need to do a group by on a date field and some other fields, but want to include in the same group rows that are identical but may have a difference of up to a minute in the date field.
    group by trunc(datefield,'MI') wouldn't work in cases like 00:00:50 and 00:01:10 (20 sec difference).
    I can do a semi join with where exists and between on the date field, but am hoping for another solution.
    any ideas?
    thanks.
    Edited by: Pyrocks on Sep 14, 2011 2:16 PM

    Hi,
    It sounds like you want something like this:
    WITH     got_new_grp     AS
         SELECT     x.*     -- or whatever columns you need
         ,     CASE
                  WHEN  datefield > MAX (datefield)
                             OVER ( ORDER BY      datefield
                                    ROWS BETWEEN  UNBOUNDED PRECEDING
                                         AND          1         PRECEDING
                                  ) + ( 1 / (24 * 60))
                  THEN  1
              END     AS NEW_GRP
         FROM     table_x     x
    --     WHERE     ...
    ,     got_grp          AS
         SELECT     n.*
         ,     COUNT (new_group) OVER (ORDER BY  datefield)     AS grp
         FROM     got_new_grp     n
    SELECT       MIN (datefield)     AS start_datefield
    ,       MAX (datefield)     AS end_datefield
    ,       COUNT (*)          AS cnt
    FROM       got_grp
    GROUP BY  grp
    ORDER BY  grp
    ;Of course, this makes assumptions for all the things you didn't explain.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data.
    Explain, using specific examples, how you get those results from that data. Be sure to include an example like the one Kim posted, where rows a and c are more than a minute apart, but both are within a minute of row b, or are connected by a chain of rows each less than 1 minute apart from its neighbor.
    Always say what version of Oracle you're using.

  • Date Difference Calculation problem

    I am creating a form which should calculate the number of days between two calender dat
    es using the Date2Sum expression but my result cell gives me
    zeo despite setting the locale and the calculation correcttly. Where could
    my problem be?
    Darlington Sakwa

    Validate the date patterns on the date fields match the date patterns used in Date2Num. For example, the attached has a start date with the display date pattern date{YYYY-MM-DD} and that matches the date pattern in my Date2Num function call.
    // form1.page1.startDateNum::calculate - (FormCalc, client)
    if (form1.page1.startDate.isNull) then
      $.rawValue = ""
    else
      $.rawValue = Date2Num(form1.page1.startDate.rawValue,"YYYY-MM-DD")
    endif
    Steve

  • How to Calculate AGE by getting difference between two Date Fields

    HI Gems
    I need to calculate AGE from getting difference from two date fields. But when i am trying to wrte fromula as Current date - date1(some date field) then it is showing error.
    How can i get values.
    Thanks
    Manu

    You already asked this question:
    How to calculate AGE from two different date fields

  • Restricting a Date field in a calculated KF

    <u>Objective</u>
    The difference between two dates: current date - date1
    <u>Issue</u>
    date1 value is 00/00/0000 for certain records and the date difference calculation is not accurate.
    <u>Question</u>
    How can I restrict the calculation for records that contain a zero value for date1?
    Thanks,
    Dave

    Just include the condition (date 1<> 0) * (curr date -date 1) in the formula you trying to calculate the date difference.

  • Calculating years between 2 date fields

    I am trying to build a form in LiveCycle Designer 7 to report missing persons.  I would like to populate a text field with the number of years between two dates entered in date/time fields; specifically using a missing person's DOB and the date they were last seen to calculate their age when last seen.
    Any assistance would be greatly appreciated.
    Thanks
    Drew

    Greetings,
    I need an age in months and years.
    On my LiveCycyle form I have two date fields 'dob' and 'rdtestdate'.  I am in Australia so we use dd/mm/yyyy as the format.
    The field designated to display the calculated result -- 'rdage' -- is set as a calculated-read only text field.
    What javascript/formcalc code would I use to calculate the age in years and months, please?
    I've been studying and testing the various solutions but don't understand well enough, sorry!  When I paste in a sample and change the field names to match mine, my result remains empty.  Sometimes I get a failed script message as I try to save the form.    I've tried quite a few different scripts to no avail.
    Thanks for any assistance.
    jeannie

  • SAP BI Date and Time Difference calculations in DSO Transformation

    Hello Guys,
    Could you please tell me how to calculate the date and Time difference between 2 fields.
    I have 2 date fields :
    Arrival Date : 6/16/2007
    Departure date : 6/19/2007
    Also i have 2 time fields for the above
    Arrival Time : 13:00:00
    Departure Time : 11:50:00
    I want to display all the four fields and 2 fields for the difference in Date and Time.
    Is it better to calcuate the differences in DSO Transformation or can you do it in the report itself.Could you please let me know the solution.
    Thanks,
    BI Consultant

    Hello Consulant BI,
    Computing the difference of two dates is easy (assuming you really just want the number of days). You can simply subtract the two dates using ABAP:
    data: w_arrival_date type sy-datum,
            w_dep_date type sy-datum,
            w_diff type i.
    w_arrival_date = <your arrival date field here>.
    w_arrival_date = <your departure field here>.
    w_diff  = w_arrival_date - w_dep_date.
    Getting the time difference isn't really much logical. I think what you want instead is to compute the totals days (and extra hours) right? If this is the case, then you can convert the date+time for both the arrival and departure  into a timestamp variable first and then t the get difference.
    Hope this helps.

  • Analyzer calculation of sum without not available data fields

    Hi,<BR><BR>I use Hyperion Analyzer 6.1.1.00206. I would like to calculate sum of some members.<BR><BR>So I did the following Analysis Tools | Calculations and selected the sum of desired members.<BR><BR>But if data field does not have a value (no data = n/a) then sum of members is also n/a (no data)!<BR><BR>Is there a way to specify the "not available" is treated as 0?<BR><BR>Thanks,<BR>Grofaty<BR>

    Yes, Click on File, then Preferences then DEFAULT FORMATTING TAB and at the bottom it gives you the option of selecting <BR>"REPLACE MISSING DATA WITH" Zero or text. I believe yours is set on text "n/a". Just select "zero" button and click ok. This should solve the problem. Best of luck<BR><BR>Benjamin M Hoffmeyer<BR>Marketing Analyst SSP CIRCLE K

  • Finding difference between Max date and Min date from single date field

    Dear Experts,
    Here I am with a scenario where i didnt find any solution in SDN and in most threads it is unanswered.
    I have 1 KF which is a date field. With reference to Serial no, I want to find out the Max and Min date from the same KF. I created 2 CKF where the same KF is used in both CKF to find the Min and Max dates,
    Ex:
    Serial No | Material | Actual Del date | Max | Min | Difference
    0123 | 300012 | 01.01.2009 | 31.01.2009 | 01.01.2009 | 30
    0123 | 300013 | 07.01.2009 | 31.01.2009 | 01.01.2009 | 30
    0123 | 300018 | 15.01.2009 | 31.01.2009 | 01.01.2009 | 30
    0123 | 300014 | 30.01.2009 | 31.01.2009 | 01.01.2009 | 30
    0124 | 300019 | 02.01.2009 | 10.01.2009 | 02.01.2009 | 8
    0124 | 300012 | 06.01.2009 | 10.01.2009 | 02.01.2009 | 8
    0124 | 300017 | 10.01.2009 | 10.01.2009 | 02.01.2009 | 8
    This is the way how I want the output where now I am able to get the values right till Max and Min and not the difference. I even created the 3rd CKF the find the difference but it is not working.
    How can I find the difference between the Max and Min dates?
    Regards,
    Chan

    Hi
    You have FM --DAYS_BETWEEN_TWO_DATES you can use this while Customer Exit.
    Try to Have 2 RKF for Min Data and Max Date and create a formula/CKF on the same ..
    Hope it helps

  • Display difference of a Date field and today's date in the query

    hi experts,
    I have a query which has a date field called 'Effective Date.'
    I just need to a add a column in the query that shows me the diffenrence between 'Effective Date' and the date the query is run (key date).
    Do i need a custom exit to accomplish this?
    The new column can either display the number days elapsed from 'Effective Date' to the key date or it can also display Number of Years/ Months / days.
    Any ideas?
    Thanks,

    Check this if it helps:
    http://www.sd-solutions.com/documents/SDS_BW_Replacement%20Path%20Variables.html

  • Locking a calculated date field

    I have a date field on my form using the custom current date object included in Designer 8.2, which applies the following FormCalc script to the LayoutReady event of the field:
    // Current Date in short-style date format.
    $.rawValue = num2date(date(), DateFmt(1))
    My problem is this: the date is set when the user fills out the form, as expected. However, whenever the form is subsequently opened, the date is updated to the current date. I need to have the field set initially when the user fills out and submits the form, and then make it static so that future reviews of the form show the initial date. I know this is probably something very simple, but I am missing it. Any suggestions?
    Thanks in advance,
    Angie

    I found this script the other day that seems to do the trick:
    if ($.rawValue==null)then
    $.rawValue = [put your script here]
    else $.rawValue
    endif

  • Calculating a date field

    Hi Everyone,
    In my input form, i have 2 date fields (effective date and end date).  I want the end date to be defaulted to 1 year after the effective date.
    How do i accomplish this??
    Thanks
    Arnil

    Hi,
    use the formula in Default value of End date field:DADD(@Effectivedate,1,'Y').
    General formula:DADD(d,n,dateunits).
    Regards,
    Govindu

Maybe you are looking for