Alpha DevCon 2018
Results 1 to 7 of 7

Thread: function timeDiff

  1. #1
    Member
    Real Name
    Robert Hoggan
    Join Date
    Mar 2010
    Location
    Scotland
    Posts
    195

    Default function timeDiff

    I hope you can help me with a problem on a function which should work properly but isn’t.

    The function is:

    function timeDiff(time1, time2) {

    if(isNaN(Date.parse(time1)) || isNaN(Date.parse(time2))) return '';

    return ( ((Date.parse(time2) -Date.parse(time1))/1000)/60 )
    }

    The problem is that if I set time1 to say 25/05/11 and time2 to say 26/05/2011 the result should be 1440 (the number of minutes in I day) however the result I am receiving is 44640 (the number of minutes in 31 days)

    I have check the date formats on the server and the dbase and they are all set to the UK format of dd/mm/yyyy

    My data connection is SQL

    Any ideas ?

    Thanks
    Bobby123

  2. #2
    Member
    Real Name
    Robert Hoggan
    Join Date
    Mar 2010
    Location
    Scotland
    Posts
    195

    Default Re: function timeDiff

    Hi Forum

    My problem with this function is the date format, the function is calulating the result based on the mm/dd/yyyy US format and my date format is the UK format dd/mm/yyyy, but i have as yet been unable to fine a working javascript answer to this which doesn't retun error.

    Has anyone found a way to format the date to mm/dd/yyyy for the calulation part of the function which would deliver the correct result, then change the date format back to the dd/mm/yyyy.

    I have looked at a lot of javascript sites and forums over the past week or so and have yet to find a workable solution to this

    thanks
    bobby123

  3. #3
    Member
    Real Name
    Robert Hoggan
    Join Date
    Mar 2010
    Location
    Scotland
    Posts
    195

    Default function timeDiff

    I am posting this question here as it is UK date related, I had posted it on the main forum and recieved no response or assistance. So I am now hoping someone in the UK will have come across the same issue and found a sulotion or can point me in the right direction.

    The function is:

    function timeDiff(time1, time2) {

    if(isNaN(Date.parse(time1)) || isNaN(Date.parse(time2))) return '';

    return ( ((Date.parse(time2) -Date.parse(time1))/1000)/60 )
    }

    The problem is that if I set time1 to say 25/05/11 and time2 to say 26/05/2011 the result should be 1440 (the number of minutes in I day) however the result I am receiving is 44640 (the number of minutes in 31 days)

    My problem with this function is the date format, the function is calulating the result based on the mm/dd/yyyy US format and my date format is the UK format dd/mm/yyyy, but i have as yet been unable to fine a working javascript answer to this which doesn't retun error.

    Has anyone found a way to format the date to mm/dd/yyyy for the calulation part of the function which would deliver the correct result, then change the date format back to the dd/mm/yyyy.

    I have looked at a lot of javascript sites and forums over the past week or so and have yet to find a workable solution to this

    My data connection is SQL

    Any ideas ?

    Thanks
    Bobby123

  4. #4
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    6,800

    Default Re: function timeDiff

    I sometimes take a sledgehammer approach to these things... specially with dates since formats can drive you crazy. So, I figure out the format the input is, then change it to what I need in code. You only need to get minutes back so you don't really need to reformat a full date back out again. You're passing dates into your function but I'm just testing here directly on a grid... other than the dates coming in, the JS should be the same for you.

    Code:
    var newDate1 = {grid.Object}.getValue('G','APPTDATE',{Grid.RowNumber});
    var newDate2 = {grid.Object}.getValue('G','APPTDATE2',{Grid.RowNumber});
    
    var date1 = new Date(newDate1.slice(3,5) + '/' + newDate1.slice(0,2) + '/' + newDate1.slice(6,10));
    var date2 = new Date(newDate2.slice(3,5) + '/' + newDate2.slice(0,2) + '/' + newDate2.slice(6,10));
    
    var yr1 = date1.getFullYear();
    var mth1 = date1.getMonth();
    var day1 = date1.getDate();
    
    var yr2 = date2.getFullYear();
    var mth2 = date2.getMonth();
    var day2 = date2.getDate();
    
    var t1 = new Date(yr1, mth1, day1, 0, 0, 0, 0);
    var t2 = new Date(yr2, mth2, day2, 0, 0, 0, 0);
    var dif = t1.getTime() - t2.getTime();
    
    var Minutes_from_T1_to_T2 = dif / 1000 / 60;
    var Minutes_Between_Dates = Math.abs(Minutes_from_T1_to_T2);
    
    alert(Minutes_Between_Dates);

  5. #5
    "Certified" Alphaholic Keith Hubert's Avatar
    Real Name
    Keith Hubert
    Join Date
    Jul 2000
    Location
    London, UK
    Posts
    6,930

    Default Re: function timeDiff

    Hi Bobby,

    I have done this with Xbasic Functions Declarations.

    After setting the variables, I get the value from the grid then Set the field value with the calculation. In my case all I wanted was the number of days. But for you to get the number of minutes multiply each day by 1440.
    Code:
    function calc_days as c (e as p)
     dim date1 as d
     dim date2 as d
     dim v_days as ndate1=e._currentRowDataNew.end_date
     date2=e._currentRowDataNew.start_date
     v_days=date1-date2
     
     e._set.days.value=v_days*1440
    end function
    Hope this gives you another point of view.
    Regards
    Keith Hubert
    Alpha Guild Member
    London.
    KHDB Management Systems
    Skype = keith.hubert


    For your day-to-day Needs, you Need an Alpha Database!

  6. #6
    Member
    Real Name
    Robert Hoggan
    Join Date
    Mar 2010
    Location
    Scotland
    Posts
    195

    Default Re: function timeDiff

    Thanks Keith

    I will give it a go, and get back to you

    Thanks again
    Bobby123

  7. #7
    Member
    Real Name
    Robert Hoggan
    Join Date
    Mar 2010
    Location
    Scotland
    Posts
    195

    Default Re: function timeDiff

    Thanks Davidk

    I will give it a try

    Thanks again

    Bobby123

Similar Threads

  1. Is it possible to call a Function from a Function?
    By OOsorio in forum Alpha Five Version 8
    Replies: 4
    Last Post: 04-21-2011, 10:17 PM
  2. MB function
    By DaveM in forum Code Archive
    Replies: 6
    Last Post: 04-13-2008, 01:53 PM
  3. Function help
    By christobal in forum Alpha Five Version 8
    Replies: 1
    Last Post: 07-07-2007, 12:55 PM
  4. is there a built command/function to replace this user Function?
    By Graham Wickens in forum Alpha Five Version 6
    Replies: 4
    Last Post: 12-07-2005, 10:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •