官术网_书友最值得收藏!

Manipulating Date Time control

There is a report that allows users to filter on Shipment Date Time using Date Time control. By default, Cognos selects current date and midnight as the date and time.

Report Studio allows you to override this with another static default value. However, business usually runs the report for the end of previous business day (5 PM).

In this recipe, we will learn how to change the default date and time for a Date Time control to the end of the previous business day.

Getting ready

Create a dummy report that shows sales quantity by shipment day. Define filter on shipment day.

How to do it...

  1. We will start by adding a date and time control to the report. For that add a new prompt page.
  2. From insertable objects, drag Date & Time Prompt on the prompt page. Connect it to the Shipment Day filter using appropriate parameter in the prompt wizard.
  3. Now select the prompt and set its Name property to shipmentDate.
    How to do it...
  4. Now add an HTML item to the prompt footer after the Finish button. Define it as:
    <script>
    function subtractDay ()
    { var dtToday = new Date();
      var dtYesterday = new Date( dtToday - 86400000 );
    // NOTE 86400000 = 24 hours * 60 (minutes per hour) * 60 (seconds per minute) * 1000 milliseconds per second)
    var strYesterday = [dtYesterday.getUTCFullYear(), dtYesterday.getMonth()+1, dtYesterday.getDate()].join("-");
    return strYesterday;
    }
    function subtractTime ()
    { var Time = "17:00:00.000"; return Time;
    }
    pickerControlshipmentDate.setValue( subtractDay() );
    timePickershipmentDate.setValue( subtractTime() );
    </script>
  5. Run the report to test. You will see that the value of the 'Date Time control' is set to previous day, 5 PM by default.

How it works...

Here we use standard JavaScript functions to work out the date of the previous day. Please note that this date is computed based on system date on user's machine.

Then we apply this date to the 'Date & Time control' using a pickerControl<name> object. Also we set the time to 5 PM using setValue function of timePicker<name> object.

You can similarly do more date and string manipulations to find First of Month, Last of Month, and so on. I found the following script on the Internet for generating commonly used dates.

<script language="JavaScript" runat="SERVER"> var today = new Date(); var thisYear = today.getYear(); var thisMonth = today.getMonth(); var thisDay = today.getDate(); function rw(s1, s2) { Response.Write("<tr><td>"+s1+"</td><td>"+s2+"</td></tr>"); } Response.Write("<table border='1'>"); rw("Today:", today.toDateString()); //Years var fdly = new Date(thisYear - 1, 0, 1); rw("First day of last year:", fdly.toDateString()); var ldly = new Date(thisYear, 0, 0); rw("Last day of last year:", ldly.toDateString()); var fdty = new Date(thisYear, 0, 1); rw("First day of this year:", fdty.toDateString()); var ldty = new Date(thisYear + 1, 0, 0); rw("Last day of this year:", ldty.toDateString()); 
var fdny = new Date(thisYear + 1, 0 ,1); rw("First day of next year:", fdny.toDateString()); var ldny = new Date(thisYear + 2, 0, 0); rw("Last day of next year:", ldny.toDateString()); //Months var fdlm = new Date(thisYear, thisMonth - 1 ,1); rw("First day of last month:", fdlm.toDateString()); var ldlm = new Date(thisYear, thisMonth, 0); rw("Last day of last month:", ldlm.toDateString()); rw("Number of days in last month:", ldlm.getDate()); var fdtm = new Date(thisYear, thisMonth, 1); rw("First day of this month:", fdtm.toDateString()); var ldtm = new Date(thisYear, thisMonth + 1, 0); rw("Last day of this month:", ldtm.toDateString()); 
rw("Number of days in this month:", ldtm.getDate()); var fdnm = new Date(thisYear, thisMonth + 1, 1); rw("First day of next month:", fdnm.toDateString()); var ldnm = new Date(thisYear, thisMonth + 2, 0); rw("Last day of next month:", ldnm.toDateString()); rw("Number of days in next month:", ldnm.getDate()); Response.Write("</table>"); </script>

There's more...

You can write more sophisticated functions to work out the previous working day instead of just the previous day.

See also

You can mix this technique with other recipes in this chapter to tie the selection event with button click or radio buttons, that is, a particular date/time can be selected when user clicks on the button or selects a radio button.

主站蜘蛛池模板: 铜陵市| 临漳县| 义马市| 宁德市| 乐陵市| 漠河县| 枣强县| 龙胜| 宜宾县| 云阳县| 自治县| 安康市| 安龙县| 禄丰县| 天长市| 堆龙德庆县| 肇州县| 苗栗市| 津市市| 吴旗县| 闸北区| 连城县| 广南县| 泾阳县| 稻城县| 永靖县| 康乐县| 桂东县| 东平县| 大名县| 沿河| 丽江市| 巴东县| 格尔木市| 洛南县| 五常市| 巫山县| 奇台县| 于都县| 左贡县| 襄垣县|