Ejemplos en Javascript

Horario y tiempo pasado en una página con un botón

Este script muestra un botón que, una vez pulsado, nos da la hora de entrada en la página, la hora corriente y el tiempo pasado en dicha página hasta ese momento.

<HTML>
<HEAD>

<TITLE>Ejemplos Javascript: ejemplo prÃ?Æ?Ã?¡ctico </TITLE>

<SCRIPT LANGUAGE = "JavaScript">

<!--  Activate Cloaking Device

//****************************************************************

// The TIMERLESS TIMER  (without graphical display)

// Developed by: Timothy Wallace,  timothy@essex1.com

//****************************************************************



var onHours = " ";

var onMinutes = " ";


var onSeconds = " ";



var offHours = 0;

var offMinutes = 0;

var offSeconds = 0;



var logSeconds = 0;

var logMinutes = 0;

var logHours = 0;



var OnTimeValue = " ";

var OffTimeValue = " ";

var PageTimeValue = " ";



// Back to previous page.

function WinOpen()

   {

   getLogoffTime();

   window.location="js-timet.htm"

   window.location="js-timet.htm"   // double call for Mac users??

   }



// Loads HTML page to full window for View Source.

function WinOpen1()

   {

   alert('nPage will load to full screen.nnUse View/Document Source from menu bar to view source.nnClose new window to return to this page. ');

   window.open("js-timer.htm","DisplayWindow","menubar=yes,scrollbars=yes");

   window.open("js-timer.htm","DisplayWindow","menubar=yes,scrollbars=yes");   // double for Macs

   }



// Captures logon time.

function getLogonTime()

   {

   var now = new Date();

   // Used to display logon time.

   var ampm = (now.getHours() >

= 12) ? " P.M." : " A.M."

   var Hours = now.getHours();

         Hours = ((Hours > 12) ? Hours - 12 : Hours);

   var Minutes = ((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes();


   var Seconds = ((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();

   // String to display log-on time.


   OnTimeValue =(" "


       + Hours


       + Minutes


       + Seconds


       + " "

       + ampm);



   // Capture logon time for use in timer().

   onHours = now.getHours();

   onMinutes = now.getMinutes();

   onSeconds = now.getSeconds()

   }



function getLogoffTime()

   {

   var now = new Date();

   // Used to display logoff time.

   var ampm = (now.getHours() >
= 12) ? " P.M." : " A.M."

   var Hours = now.getHours();

         Hours = ((Hours > 12) ? Hours - 12 : Hours);


   var Minutes = ((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes();


   var Seconds = ((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();

   // String to display log-off time.


   OffTimeValue =(" "


       + Hours


       + Minutes


       + Seconds


       + " "

       + ampm);



   // Capture logoff time for use in timer().

   offHours = now.getHours();

   offMinutes = now.getMinutes();

   offSeconds = now.getSeconds();



   timer();

   }



// Compute difference between logoff time and logon time.

function timer()

   {

   if (offSeconds >
= onSeconds)

      { logSeconds = offSeconds - onSeconds; }

   else

      {

      offMinutes -= 1;

      logSeconds = (offSeconds + 60) - onSeconds;     

      }

   if (offMinutes >= onMinutes)

      { logMinutes = offMinutes - onMinutes; }

   else

      {

      offHours -= 1;

      logMinutes = (offMinutes + 60) - onMinutes;

      }

   logHours = offHours - onHours;



   // Used to display time on page.

   logHours =  ((logHours < 10) ? "0" : ":") + logHours;

   logMinutes = ((logMinutes < 10) ? ":0" : ":") + logMinutes;


   logSeconds = ((logSeconds < 10) ? ":0" : ":") +logSeconds;

   // String to display time on page.

   PageTimeValue =(" "


       + logHours


       + logMinutes


       + logSeconds);



   displayTimes();

   }



function displayTimes()

   {

   alert("nLOG ON TIME    : " +OnTimeValue+"nnLOG OFF TIME  : "+OffTimeValue+"nnTIME ON PAGE : " + PageTimeValue);

   }



// Deactivate Cloaking -->


</SCRIPT>


</HEAD>

<BODY bgcolor="white" onLoad="getLogonTime()";>


<FORM>

<INPUT TYPE="button" value="Tiempo pasado en esta pÃ?Æ?Ã?¡gina" onClick="getLogoffTime()">

</FORM>


</body>
</html>

Fechas y horarios