×
Namespaces

Variants
Actions
(Difference between revisions)

Showing date and time in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
seppo_fn (Talk | contribs)
tapla (Talk | contribs)
Line 9: Line 9:
 
|subcategory=Base/system
 
|subcategory=Base/system
 
|creationdate=December 18, 2008
 
|creationdate=December 18, 2008
|keywords=Date, getFullYear, getMonth
+
|keywords=Date, Date.getFullYear(), Date.getMonth(), Date.getDate(), Date.getHours(), Date.getMinutes(), Date.getSeconds()
 
}}
 
}}
 
  
 
==Overview==
 
==Overview==
This code snippet shows how to receive current system date and time using S60 Platform Services for Web Runtime, introduced in S60 5th Edition.
 
  
To obtain access to the date object use the <tt>Date()</tt> method.  After receiving this object, use getFullYear(), getMonth(), and so on, methods to extract the needed information.
+
This code snippet shows how to receive current system date and time using JavaScript's own methods.
 +
 
 +
==Source==
  
==Source file: clock.html==
+
Onto the HTML page, add the components into which the time will be printed:
  
 
<code xml>
 
<code xml>
<?xml version="1.0" encoding="UTF-8"?>
+
<p id="standardTime"></p>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+
<p id="customTime"></p>
                            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
<html xmlns="http://www.w3.org/1999/xhtml">
+
    <head>
+
        <script type='text/javascript' src="Clock.js"/>
+
        <title></title>
+
    </head>
+
    <body onload="init()">
+
        <h3> Time </h3>
+
        <!-- This div will be later used for displaying time -->
+
        <div id="txt"></div>
+
        <h3> Parameters </h3>
+
        <!-- Form for choosing the display type -->
+
        <form id="dysplay_type" action="">
+
        <div>
+
            <input name="system_output" type="radio" onClick="setStandard( )"/>
+
                System output
+
            <input name="custom_output" type="radio" onClick="setCustom( )"/>
+
                Custom output
+
        </div>
+
        </form>
+
        <!-- Form for choosing the display information for the custom display type -->
+
        <form id="params" action="">
+
        <div>
+
            <input name="Date" type="checkbox"/>Date
+
            <input name="Weekday" type="checkbox"/>Weekday
+
            <input name="Time" type="checkbox"/>Time
+
            <input name="MSeconds" type="checkbox"/>MSeconds
+
        </div>
+
        </form>
+
    </body>
+
</html>
+
 
+
 
</code>
 
</code>
  
==Source file: clock.js==
+
The following functions handle the displaying of time:
  
 
<code javascript>
 
<code javascript>
 
// Enum defines how to represent the information
 
var timeShowType = { system : 0, custom : 1, other : 2 };
 
 
// Array containing the weekday names
 
var weekDays = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
 
    "Thursday", "Friday", "Saturday");
 
 
// Current data representation type
 
var viewType; 
 
 
var clockRefreshInterval = 207;
 
 
 
/**
 
/**
  * Setting default data on application load
+
  * Displays the time in both standard (received from the system) and custom
 +
* (built manually) formats.
 
  */
 
  */
function init() {
+
function displayTime() {
     // Setting information layout to custom
+
     // Get current date info
    viewType = timeShowType.custom;
+
     var today = new Date();
    // Setting checkboxes marks
+
    document.getElementById('dysplay_type').custom_output.checked = true;
+
    document.getElementById('params').Date.checked = true;
+
    document.getElementById('params').Time.checked = true;
+
       
+
    // Create main menu items
+
     var standardData = new MenuItem("System", 11);
+
    var custom = new MenuItem("Custom", 12);
+
 
      
 
      
     // Setting handlers for main menu items
+
     // Standard format
     standardData.onSelect = setStandard;
+
     document.getElementById("standardTime").innerHTML = today;
    custom.onSelect = setCustom;
+
  
     // Adding items to menu
+
     // Custom format
     menu.append(standardData);
+
     var y = today.getFullYear();
     menu.append(custom);
+
     var m = today.getMonth() + 1; // Starts from 0
 
+
    var d = today.getDate();
     // Start displaying menu
+
     var hh = today.getHours();
     menu.showSoftkeys();
+
    var mm = today.getMinutes();
 +
     var ss = today.getSeconds();
 
      
 
      
     // Start timer for time displaying
+
     // Add a zero in front of numbers < 10
     setInterval( 'startTime()', clockRefreshInterval );
+
     m = padTime(m);
     startTime();
+
    d = padTime(d);
}
+
     mm = padTime(mm);
 +
    ss = padTime(ss);
  
/**
+
     var result = y + "-" + m + "-" + d + " " +
* Set data display type to system
+
                weekDays[today.getDay()] + " " +
*/
+
                hh + ":" + mm + ":" + ss;
function setStandard() {
+
     document.getElementById('dysplay_type').system_output.checked = true;
+
    document.getElementById('dysplay_type').custom_output.checked = false;
+
    document.getElementById('params').style.display = "none";
+
    viewType = timeShowType.system;
+
}
+
  
/**
+
     document.getElementById("customTime").innerHTML = result;
* Set data display type to custom
+
*/
+
function setCustom() {
+
     document.getElementById('dysplay_type').system_output.checked = false;
+
    document.getElementById('dysplay_type').custom_output.checked = true;
+
    document.getElementById('params').style.display = "block";
+
    viewType = timeShowType.custom;
+
 
}
 
}
 
+
   
/**
+
  * Function repeatedly called for updating current time info
+
*/
+
function startTime() {
+
    // Get current date info
+
    var today = new Date();
+
    // Laying out data
+
    if(viewType == timeShowType.system) {
+
        // Default layout of data
+
        document.getElementById('txt').innerHTML = Date();
+
    } else if (viewType == timeShowType.custom) {
+
        var result = "";
+
        // Add date to result string
+
        if( document.getElementById('params').Date.checked == true ) {
+
            result += today.getFullYear() + "/" + today.getMonth() + "/" +
+
                    today.getDate() + " ";
+
        }
+
        // Add week day to result string
+
        if(document.getElementById('params').Weekday.checked == true) {
+
            result += weekDays[today.getDay()] + " ";
+
        }
+
        // Add time to result string
+
        if(document.getElementById('params').Time.checked == true) {
+
            var h=today.getHours();
+
            var m=today.getMinutes();
+
            var s=today.getSeconds();
+
            // Add a zero in front of numbers<10
+
            m=checkTime(m);
+
            s=checkTime(s);
+
            result += h + ":" + m + ":" + s + ".";
+
        }
+
        // Add milliseconds to result string
+
        if(document.getElementById('params').MSeconds.checked == true) {
+
            result += today.getMilliseconds();
+
        }
+
        // receive element 'txt' in DOM and set its html text
+
        document.getElementById('txt').innerHTML = result;
+
    }
+
}
+
 
+
 
/**
 
/**
 
  * If time is represented by one digit, add '0' in front of it.
 
  * If time is represented by one digit, add '0' in front of it.
  * @param string to convert to two digits
+
  * @param i the number to convert to two digits
  * @return string representing number in two or more digits
+
  * @return a string representing the number in two or more digits
 
  */
 
  */
function checkTime(i) {
+
function padTime(i) {
     if(i < 10) {
+
     if (i < 10) {
 
         i = "0" + i;
 
         i = "0" + i;
 
     }
 
     }
Line 177: Line 73:
 
</code>
 
</code>
  
 +
Add also the call to <tt>setInterval</tt>, which in this case updates the time in every half seconds.
 +
 +
<code javascript>
 +
setInterval("displayTime()", 500);
 +
displayTime();
 +
</code>
  
 
==Postconditions==
 
==Postconditions==
* Shows custom layout of clock upon loading.
 
* You can choose between default (system) and custom (custom) clock view by selecting the respective radiobox.
 
* You can choose what information you want to see in custom view by marking checkboxes.
 
  
 +
Time is displayed on the HTML page in both standard (received from the system) and custom (built manually) formats.
  
 
==Supplementary material==
 
==Supplementary material==
 +
 
You can view the source file and executable application in the attached ZIP archive. The archive is available for download at [[Media:Showing_date_and_time_in_WRT.zip]].
 
You can view the source file and executable application in the attached ZIP archive. The archive is available for download at [[Media:Showing_date_and_time_in_WRT.zip]].
  
[[Category:Web Runtime (WRT)]][[Category:Code Examples]][[Category:PIM]][[Category:S60 5th Edition]]
+
[[Category:Web Runtime (WRT)]][[Category:Code Examples]]

Revision as of 10:11, 16 January 2009


Template:KBCS

Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 5th Edition
Article
Keywords: Date, Date.getFullYear(), Date.getMonth(), Date.getDate(), Date.getHours(), Date.getMinutes(), Date.getSeconds()
Created: (18 Dec 2008)
Last edited: tapla (16 Jan 2009)

Overview

This code snippet shows how to receive current system date and time using JavaScript's own methods.

Source

Onto the HTML page, add the components into which the time will be printed:

<p id="standardTime"></p>
<p id="customTime"></p>

The following functions handle the displaying of time:

/**
* Displays the time in both standard (received from the system) and custom
* (built manually) formats.
*/

function displayTime() {
// Get current date info
var today = new Date();
 
// Standard format
document.getElementById("standardTime").innerHTML = today;
 
// Custom format
var y = today.getFullYear();
var m = today.getMonth() + 1; // Starts from 0
var d = today.getDate();
var hh = today.getHours();
var mm = today.getMinutes();
var ss = today.getSeconds();
 
// Add a zero in front of numbers < 10
m = padTime(m);
d = padTime(d);
mm = padTime(mm);
ss = padTime(ss);
 
var result = y + "-" + m + "-" + d + " " +
weekDays[today.getDay()] + " " +
hh + ":" + mm + ":" + ss;
 
document.getElementById("customTime").innerHTML = result;
}
 
/**
* If time is represented by one digit, add '0' in front of it.
* @param i the number to convert to two digits
* @return a string representing the number in two or more digits
*/

function padTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}

Add also the call to setInterval, which in this case updates the time in every half seconds.

setInterval("displayTime()", 500);
displayTime();

Postconditions

Time is displayed on the HTML page in both standard (received from the system) and custom (built manually) formats.

Supplementary material

You can view the source file and executable application in the attached ZIP archive. The archive is available for download at Media:Showing_date_and_time_in_WRT.zip.

111 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×