If you are unable to create a new account, please email support@bspsoftware.com

 

News:

MetaManager - Administrative Tools for IBM Cognos
Pricing starting at $2,100
Download Now    Learn More

Main Menu

Calculate time difference between two Date & Time Prompts

Started by ordotan, 29 Jul 2013 02:12:01 AM

Previous topic - Next topic

ordotan

Hi,

I have in my prompt page two 2 Date & Time Prompts, and I need to calculate in JS the difference in Miliseonds between the two.

Any suggestions on how to implement it?

calson33


ordotan

I need to perform some selections in the prompt page based on the difference between the inputs for the two Date&Time prompts (which stand for "Start Time" and "End Time" filters for the report)

CognosPaul

I have a vision that something similar was already implemented by a consultant. In a JS file that can be found somewhere in the webapps folder. Find the relevant code that and paste it in here. I'll see if I can upgrade it.

ordotan

I'm not sure that I understand what you mean...
Anyway, the code that we have (works ok in cognos 8.2) is below here.

The problem is with all this type of references, that aren't compatible with cognos 10.2
document.formWarpRequest._oChoiceBeginDate[0].value
document.formWarpRequest.hoursBeginDate.value
...
....


<script type="text/javascript">

var MINUTE_MILLIS = 1000 * 60;
var HOUR_MILLIS = MINUTE_MILLIS * 60;
var DAY_MILLIS = HOUR_MILLIS * 24;
var WEEK_MILLIS = DAY_MILLIS * 7;
var MONTH_MILLIS = DAY_MILLIS * 30;
var YEAR_MILLIS = MONTH_MILLIS * 12;
var mForm = document.formWarpRequest;
TableKey_Box=document.formWarpRequest._oLstChoices_key;

function setShowPer()

{
            var liIndex = mForm._oLstChoices_li.selectedIndex
            var timeRangeMillis = liIndex > 1 ? getTimeRangeMillisByLi() : getTimeRangeMillisByCalenders();
            setTableKeyByReportTimeRange(timeRangeMillis);
}

function myFinish()
{

var liIndex = mForm._oLstChoices_li.selectedIndex
var timeRangeMillis = liIndex > 2 ? getTimeRangeMillisByLi() : getTimeRangeMillisByCalenders();
if (TableKey_Box[2].selected) {
setTableKeyByReportTimeRange(timeRangeMillis);
}
promptButtonFinish();
}

function getTimeRangeMillisByCalenders()
{

if (mForm.AMPMBeginDate)  { //to support system with AM-PM Time format
var beginTime = constructDateAMPM(mForm._oChoiceBeginDate[0].value, mForm.hoursBeginDate.value,
                        mForm.minutesBeginDate.value, mForm.AMPMBeginDate.value);
            var endTime = constructDateAMPM(mForm._oChoiceEndDate[0].value, mForm.hoursEndDate.value,
                        mForm.minutesEndDate.value, mForm.AMPMEndDate.value);
}
else    //to support system with 24-hour Time format
{
var beginTime = constructDate(mForm._oChoiceBeginDate[0].value, mForm.hoursBeginDate.value,
mForm.minutesBeginDate.value);
var endTime = constructDate(mForm._oChoiceEndDate[0].value, mForm.hoursEndDate.value,
mForm.minutesEndDate.value);
}
return endTime.getTime() - beginTime.getTime();
}

function constructDate(dateStr, hoursStr, minutesStr) //to support system with 24-hour Time format
{
var year = parseInt(dateStr.substr(0,4),10);
var month = parseInt(dateStr.substr(5,2),10) - 1;
var day = parseInt(dateStr.substr(8,2),10);
var hours = parseInt(hoursStr,10);
var minutes = parseInt(minutesStr,10);
return new Date(year, month, day, hours, minutes, 0, 0);

}

function constructDateAMPM(dateStr, hoursStr, minutesStr, amPm) //to support system with AM-PM Time format

{

            var year = parseInt(dateStr.substr(0,4),10);
            var month = parseInt(dateStr.substr(5,2),10) - 1;
            var day = parseInt(dateStr.substr(8,2),10);
            var hours = parseInt(hoursStr,10) % 12 + (amPm == 'AM' ? 0 : 12);
            var minutes = parseInt(minutesStr,10);
            return new Date(year, month, day, hours, minutes, 0, 0);

}

function setTableKeyByReportTimeRange(timeRangeMillis)
{
var index = timeRangeMillis <= 2 * HOUR_MILLIS ? 3 :
timeRangeMillis <= 2 * DAY_MILLIS ? 4 :
timeRangeMillis <= 2 * MONTH_MILLIS ? 5 :
timeRangeMillis <= 3 * MONTH_MILLIS ? 6 :7;
mForm._oLstChoices_key.selectedIndex = index;
listBox_key.checkData();
}

function getTimeRangeMillisByLi()
{
var selectedIndex = mForm._oLstChoices_li.selectedIndex;
var liText = mForm._oLstChoices_li.options[selectedIndex].text;
var quantity = parseInt(liText.match(/[\d]+/));
var unit = liText.match(/minute|hour|day|week|month|year/i)[0].toLowerCase();

return quantity * (unit == "minute" ? MINUTE_MILLIS :
unit == "hour" ? HOUR_MILLIS :
unit == "day" ? DAY_MILLIS :
unit == "week" ? WEEK_MILLIS :
unit == "month" ? MONTH_MILLIS : YEAR_MILLIS);
}


</script>

MFGF

Quote from: ordotan on 30 Jul 2013 04:34:34 AMI'm not sure that I understand what you mean...

If I'm reading this right, the consultant who wrote that code originally has the initials PM :)

MF.
Meep!

ordotan

Paul, again you saved the day  :P

The following JS function calculates the difference in ms between two date prompts:


function getTimeRangeMillisByCalendar(startDate, endDate)

{

  // Get start date and time and convert to JS Date.
  var sD = window['pickerControl'+startDate].m_oForm.value.split('-');

  // subtract 1 from month (0 is January, 11 is December)
  sD[1]-=1;
  var sT = window['timePicker'+startDate].m_oForm.value.split(':');
  var sDT = new Date(sD[0],sD[1],sD[2],sT[0],sT[1]);

  // Get end date and time and convert to JS Date.
  var eD = window['pickerControl'+endDate].m_oForm.value.split('-');

  // subtract 1 from month (0 is January, 11 is December)
  eD[1]-=1;
  var eT = window['timePicker'+endDate].m_oForm.value.split(':');
  var eDT = new Date(eD[0],eD[1],eD[2],eT[0],eT[1]);

  //find and return dfference between the dates
  return eDT - sDT;

}