Want to get rid of Google Ads, click here.
+ Reply to Thread
Results 1 to 15 of 15

Thread: Subtract dates in javascript

  1. #1
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default Subtract dates in javascript

    Can anybody tell me how this normal expression is done in javascript

    Code:
     
    $negdatestamp='01/01/2200 01:00:00' - tod()
    so it returns a date in the format '12345 00:00:00'
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  2. #2
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default

    Ok I finally found a way to do this. Its a bit clumsy but works.

    Here is the full script that add a custom activity record to any activity file


    Code:
    // This script add a custom activity record.
    // 
    function addActivity(record, activitytype, activitytext)
    {
     
    var strFileName =system.functions.filename( record );
    var strRecordID;
    var strActivityFilename;
    strRecordID = record.number ;
    switch (strFileName)
    {
    case "probsummary":
    strRecordID = record.number ;
    strActivityFilename ="activity";
    break;
    case "cm3r":
    strRecordID = record.number ;
    strActivityFilename ="activitychanges";
    break;
    case "incidents":
    strRecordID = record.incident_id ;
    strActivityFilename ="activityservicemgt";
    break;
    case "rootcause":
    strRecordID = record.id ;
    strActivityFilename ="activityproblem";
    break;
    case "knownerror":
    strRecordID = record.id ;
    strActivityFilename ="activityknownerror";
    break;
    default:
    break;
    }
    var activityFile =new SCFile(strActivityFilename);
    var strSqlActivity;
    var arrDescription =new Array();
    var now =new Date();
    var future =new Date(2200,1,1,1);
    var delta = future -now - (30*86400000);
    var days =String(parseInt(delta/86400000));
    var hours =String(parseInt((delta - (days*86400000))/3600000));
    var minutes =String(parseInt((delta - (days*86400000) - (hours*3600000))/60000));
    var seconds =String(parseInt((delta - (days*86400000) - (hours*3600000) - (minutes*60000) )/1000));
    var negdatestamp =new XMLDate( "P"+days+"DT"+hours+"H"+minutes+"M"+seconds+"S");
    activityFile.number = strRecordID;
    activityFile.type = activitytype;
    activityFile.operator =system.functions.operator();
    activityFile.datestamp = now;
    activityFile.negdatestamp =negdatestamp.getDatum();
    arrDescription[0]= activitytext;
    activityFile.description = arrDescription;
     
    activityFile.doInsert();
    }
    This
    Code:
    var now =newDate();
    var future =newDate(2200,1,1,1);
    var delta = future -now - (30*86400000);
    var days =String(parseInt(delta/86400000));
    var hours =String(parseInt((delta - (days*86400000))/3600000));
    var minutes =String(parseInt((delta - (days*86400000) - (hours*3600000))/60000));
    var seconds =String(parseInt((delta - (days*86400000) - (hours*3600000) - (minutes*60000) )/1000));
    var negdatestamp =newXMLDate( "P"+days+"DT"+hours+"H"+minutes+"M"+seconds+"S");
    and this

    Code:
    activityFile.negdatestamp =negdatestamp.getDatum();
    is the code that replaces that simple rad expression from first post.
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  3. #3
    Member
    Join Date
    Feb 2002
    Location
    Phoenix, AZ
    Posts
    34

    Default

    Did you ever find an answer to this?
    Jeff

    Quote Originally Posted by tommy View Post
    Can anybody tell me how this normal expression is done in javascript

    Code:
     
    $negdatestamp='01/01/2200 01:00:00' - tod()
    so it returns a date in the format '12345 00:00:00'

  4. #4
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default

    Unfortunately not.
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  5. #5
    Senior Member glg's Avatar
    Join Date
    Aug 2004
    Location
    Chicago, IL, USA
    Posts
    714

    Default

    nevermind, user error

  6. #6
    Senior Member glg's Avatar
    Join Date
    Aug 2004
    Location
    Chicago, IL, USA
    Posts
    714

    Default

    Hey -

    I've been running this through some testing and found a minor problem. In RAD, the calculation of 1/1/2200 00:00 - tod() appears to be adjusted for timezone. ie, if I go into the sc.activity RAD app with an operator set to US/Eastern, the calculation is 12/31/2100 19:00 - tod(). The result is that when I used this function, activities added via javascript and ones added via activityaction are just slightly off. I had to make a few corrections to fix this.

    Replace these two lines:

    Code:
    var future =newDate(2200,1,1,1);
    var delta = future -now - (30*86400000);
    with

    Code:
    var future = new Date(2200,1,1);
    var offset = future.getTimezoneOffset();
    var offsetmsec = offset * 60 * 1000;
    var delta = future - now - (30 * 86400000) - offsetmsec;
    I tested this in a 6.1.2 system.

  7. #7
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default

    Thanks.
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  8. #8
    Senior Member tuncay's Avatar
    Join Date
    Jul 2004
    Location
    Germany
    Posts
    113

    Post time in query

    I solved it similar. I need the calculated time (today - 1 day) in a select query:

    var alert = new SCFile("Alert");
    var XDate = new XMLDate( new Date() );
    var SCNow = XDate.getSCDateTimeString(); // es soll nicht einfach sein: erst wandeln dann handeln
    var X5 = new Date();
    // 86400000 = 24 h * 60 min * 60 s * 1000 ms
    X5.setTime(X5.getTime() - 24 * 60 * 60 * 1000);
    var XDate5 = new XMLDate( X5 );
    var SCNow5 = XDate5.getSCDateTimeString(); // es soll nicht einfach sein: erst wandeln dann handeln

    var rc = alert.doSelect("alert.name#\"Email\" and alert.time<='" + SCNow + "' and alert.time>='" + SCNow5 + "'"); // ein Tag Fenster prüfen

  9. #9
    Junior Member
    Join Date
    Dec 2007
    Location
    Johannesburg
    Posts
    4

    Default JavaScript Date/Time Calculations... WITH Calendars

    Greetings All,
    Has anyone out there managed to perform JS date/time calculations with the work calendars functionality that you normally get out of RAD apps like:
    calendar.calc.date.pos|neg|fc

    I have been tearing my hair out trying 'callrad' to call the RAD apps as well as trying to call scripts that call the RAD apps but I am getting a stone wall each and every time.
    Thanks.

  10. #10
    Junior Member
    Join Date
    Jul 2008
    Posts
    25

    Default

    Hi guys, i am creating activity records via JScript but have a bit of a problem. Wondered if any of you have came up with the solution for this one:

    SC stores the negdatestamp as something like this: 71013 03:11:15
    I calculated it as mentioned above but come up with something like this: 07/09/2161 03:30:31

    The question is how to transform my calculation to the right format.

    My thanks in advance

  11. #11
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default

    Quote Originally Posted by rdaniel View Post
    Hi guys, i am creating activity records via JScript but have a bit of a problem. Wondered if any of you have came up with the solution for this one:

    SC stores the negdatestamp as something like this: 71013 03:11:15
    I calculated it as mentioned above but come up with something like this: 07/09/2161 03:30:31

    The question is how to transform my calculation to the right format.

    My thanks in advance
    See post #2. I have not come up with a different way.
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  12. #12
    Junior Member
    Join Date
    Jul 2008
    Posts
    25

    Default Solved it

    ok so the problem wasn't so big after all. All I did was this:

    Code:
    var now =new Date();
    var future =new Date(2200,1,1,1);
    var delta = future -now - (30*86400000)-3600000;
    var days =String(parseInt(delta/86400000));
    var hours =String(parseInt((delta - (days*86400000))/3600000));
    var minutes =String(parseInt((delta - (days*86400000) - (hours*3600000))/60000));
    var seconds =String(parseInt((delta - (days*86400000) - (hours*3600000) - (minutes*60000) )/1000));
    
    if(hours<10)
        hours="0"+hours;
    if(minutes<10)
        minutes="0"+minutes;
    if(seconds<10)
        seconds="0"+seconds;
    
    var negdatestamp =(days+" "+hours+":"+minutes+":"+seconds);
            
    var date1 = new Date();
                        
    date1=system.functions.val(negdatestamp,3);
                        
    fActivity.negdatestamp = date1;
    works like a charm.

    I was trying to force it to store a date in this format: 71013 03:11:15 but it was not needed since it stores it in the table in the correct format.
    Last edited by rdaniel; 2008-07-24 at 18:18.

  13. #13
    Senior Member glg's Avatar
    Join Date
    Aug 2004
    Location
    Chicago, IL, USA
    Posts
    714

    Default

    I've been using what tommy wrote in post 2 with my modification in post 6 for a while now without any issues.

  14. #14
    Junior Member
    Join Date
    Jun 2010
    Posts
    1

    Default Negative Timestamp Fix for SM 9.20 Activity Updates Sort Order

    We are using Service Manager 9.20. I had to make a slight modifications to Tommy's script to get the negative timestamp calculation to work out correctly for ordering of activity updates.

    Eventually I found the answer to my calculation problem in the RAD application update.change.activity.

    The future date calculation in our system is based on '12/31/2199 10:00:00' instead of '01/01/2200 01:00:00'

    The fix was a simple change to one line in the script posted by Tommy above.

    Change line:
    Code:
    var future =new Date(2200,1,1,1);
    To:
    Code:
    var future = new Date(2199,12,31,10);

  15. #15

    Default

    The code:

    var now =new Date();
    var future =new Date(2200,1,1,1);
    fActivity.negdatestamp = system.functions.val(((future - now)/1000),3);

    Worked fine for me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

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