×
Namespaces

Variants
Actions
(Difference between revisions)

Showing date and time in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Automated change of category from Web Runtime (WRT) to Symbian Web Runtime)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
Line 2: Line 2:
 
__NOEDITSECTION__
 
__NOEDITSECTION__
 
{{KBCS}}
 
{{KBCS}}
{{CodeSnippet
+
{{ArticleMetaData
 
|id=CS001229
 
|id=CS001229
 
|platform=S60 5th Edition
 
|platform=S60 5th Edition
Line 10: Line 10:
 
|creationdate=December 18, 2008
 
|creationdate=December 18, 2008
 
|keywords=Date, Date.getFullYear(), Date.getMonth(), Date.getDate(), Date.getHours(), Date.getMinutes(), Date.getSeconds()
 
|keywords=Date, Date.getFullYear(), Date.getMonth(), Date.getDate(), Date.getHours(), Date.getMinutes(), Date.getSeconds()
 +
 +
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices. -->)
 +
|author=[[User:MiGryz]]
 
}}
 
}}
  

Revision as of 11:41, 24 June 2011


Template:KBCS

Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 5th Edition
Platform Security
Capabilities: )
Article
Keywords: Date, Date.getFullYear(), Date.getMonth(), Date.getDate(), Date.getHours(), Date.getMinutes(), Date.getSeconds()
Created: MiGryz (18 Dec 2008)
Last edited: hamishwillee (24 Jun 2011)

Overview

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

Source

Add the components where the time will be printed on the HTML page:

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

The following functions handle displaying the time:

/**
* Displays the time in both standard (received from the system) and custom
* (manually built) 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;
}

Also add the call to setInterval which in this case updates the time every 0.5 seconds.

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

Postconditions

Time is displayed on the HTML page in both standard (received from the system) and custom (manually built) 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.

108 page views in the last 30 days.
×