This discussion is archived
6 Replies Latest reply: May 3, 2013 1:19 AM by VC RSS

Best way to display current date time on each page

scot Newbie
Currently Being Moderated
Hi. I've tried searching the forum for this, but these are very common search terms, and while I've learned a bit from the threads I've found, I haven't found my specific answer. I'm happy to read a thread if pointed in the right direction. I'm using APEX 3, and as will become evident shortly, I'm quite new at it.

My Question: I'm looking for the best/preferred way to display the current date and time on each page in an application. Or at least, each page in an application that uses one specific page template. I'm using theme 16, Dark Blue, and the page template of interest is One Level Tabs with Sidebar.

An added complication is that I'm not wanting to display it in one of the standard region position numeric place holders that the template defines. Instead, there is a little blue box in a cell towards the top left of the page that would be to the left of the breadcrumb, and above the sidebar region. There is actually a label in the td code for this cell; class="t16SideBarTitle"

I put some static text there in that cell as a test and it shows up perfectly where I want it. Now, this date time would need to be refreshed with each page. The question is how best to accomplish it.

My current attempt is to create a hidden Item on Page 0, called P0_DATE, and to put the text "&P0_DATE." right there in the cell I want. The item itself is defined to have a "source value or expression" of:
to_char(sysdate,'Dy, Mon DD HH:MI:SS PM')
The "Source Used" is "Always, replacing any value in session state"
The "Source Type" is PLSQL Expression or function"

Now, this method comes close. It displays the date and time right where I want it, in the format I want it. And when I click on different tabs in the app, it refreshes the date and time as I'd like. But it doesn't refresh when I refresh the page, or when I click on the breadcrumb entry to the page, or when I click on the navigation list to different pages that I have placed in the sidebar region.

When searching this forum, I found another possible approach, a reference to Application Items. So another option might be to create an appication item to hold the datetime. My hesitation in doing so is that when you click on application items from the shared components page, the first line of the help text is: "Use Application Items to maintain session state". I don't want to maintain session state. I want this value to be reset each time a page is clicked on, or each time the current page is refreshed.

But, if I did create an application item, it looks like I could create a corresponding application process to load the value in an anonymous plsql block on load before header. With something like select sysdate from dual, although I'm not sure yet how to get it into my application item.

Or, it looks like I could create an application computation. It looks like I could use the same idea as the hidden page 0 item above, with the same plsql expression. I would then set the computation point to before header. But if I did, should the computation item be an application item, or the hidden page 0 item from before?

So to recap, and thanks for having the patience to make it this far, should I:

a) use a hidden page 0 item and alter how I'm doing it to make it work
b) use an application item with an appication process
c) use an appication computation with an application item
d) use an appication computation with a hideen page 0 item
e) use some prebuilt way of doing this that I don't know
f) use some other custom alternative

And with all of the approaches, is using the substitution of "&ITEM." directly in the page template the preferred way to go, since I don't want to use an existing region position or page body substition point? Should I instead create a new substituion point assuming that it is possible to do so?

Thanks.
  • 1. Re: Best way to display current date time on each page
    ATD Guru
    Currently Being Moderated
    Hi,

    The simplest thing would be to create an Application Item and an Application Computation that populates this with the date/time and is set to run "Before Header". You can then use the &item. notation anywhere within your page template.

    But, as you have noticed, using the browser's navigation/refresh will not necessarily update the date/time value - this is because the value is retrieved from the browser's cache rather than being rerendered by Apex so will only show the date/time of when the page was first loaded.

    If this causes you problems, you can get around it by using javascript. Add the following at the location where you want to display the date/time:

    <span id="ShowDateTime"> </span>
    <script type="text/javascript">
    function showDatetime()
    {
    var d = new Date();
    var i = document.getElementById("ShowDateTime");
    i.innerHTML = d.toLocaleString();
    setTimeout("showDatetime()", 1000);
    }
    showDatetime();
    </script>

    This will insert the date/time on the page and keep updating it every second (the 1000 in setTimeout refers to milliseconds - 1000 = 1 second).

    Andy
  • 2. Re: Best way to display current date time on each page
    scot Newbie
    Currently Being Moderated
    Thanks Andy that is excellent. I've tried the Application Computation and it works well. I'll look into the Javascript portion next.
  • 3. Re: Best way to display current date time on each page
    user7686981 Newbie
    Currently Being Moderated
    Hi Andi:
    I tried the Java Script example by following your instructions and it works great for me!
    I am trying to create a simple application template for educational purposes for myself, where I describe various alternatives of design and programming of APEX web pages.
    I have a quick question: When I try to place the Java Script Code in the region's header and place the span within my paragraph, it did not work.
    What am I missing ?
    Thanks so much,
    Alice G.
  • 4. Re: Best way to display current date time on each page
    VC Guru
    Currently Being Moderated
    ATD wrote:
    Hi,

    The simplest thing would be to create an Application Item and an Application Computation that populates this with the date/time and is set to run "Before Header". You can then use the &item. notation anywhere within your page template.

    But, as you have noticed, using the browser's navigation/refresh will not necessarily update the date/time value - this is because the value is retrieved from the browser's cache rather than being rerendered by Apex so will only show the date/time of when the page was first loaded.

    If this causes you problems, you can get around it by using javascript. Add the following at the location where you want to display the date/time:

    <span id="ShowDateTime"> </span>
    <script type="text/javascript">
    function showDatetime()
    {
    var d = new Date();
    var i = document.getElementById("ShowDateTime");
    i.innerHTML = d.toLocaleString();
    setTimeout("showDatetime()", 1000);
    }
    showDatetime();
    </script>

    This will insert the date/time on the page and keep updating it every second (the 1000 in setTimeout refers to milliseconds - 1000 = 1 second).

    Andy
    Andy,

    But doesn't the JavaScript solution show the date time from client browser where as the APEX application item will fetch from the server, and these both date time (client/browser) don't necessarily be the same?

    Cheers,
    Vikram
  • 5. Re: Best way to display current date time on each page
    jwellsnh Expert
    Currently Being Moderated
    Vikram, this is a 6 year old thread. Andy has not been active on this forum for several years now.

    Jeff
  • 6. Re: Best way to display current date time on each page
    VC Guru
    Currently Being Moderated
    jwellsnh wrote:
    Vikram, this is a 6 year old thread. Andy has not been active on this forum for several years now.

    Jeff
    Sorry Jeff. yes it was and didn't realize until I pressed the Post Message