×
Namespaces

Variants
Actions
(Difference between revisions)

Showing date and time in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
tapla (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Abstract. Tidy wiki text)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Symbian Web Runtime]][[Category:General Programming]][[Category:Base/System]][[Category:Code Examples]][[Category:JavaScript]]
__NOEDITSECTION__
+
{{Abstract|This code example shows how to receive the current system date and time using JavaScript's own methods.}}
{{KBCS}}
+
 
{{CodeSnippet
+
{{ArticleMetaData <!-- v1.2 -->
|id=CS001229
+
|sourcecode= [[Media:Showing date and time in WRT.zip]]
|platform=S60 5th Edition
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices=Nokia 5800 XpressMusic
+
|devices= Nokia 5800 XpressMusic
|category=Web Runtime (WRT)
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|subcategory=Base/system
+
|platform= S60 5th Edition
|creationdate=December 18, 2008
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|keywords=Date, Date.getFullYear(), Date.getMonth(), Date.getDate(), Date.getHours(), Date.getMinutes(), Date.getSeconds()
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= Date, Date.getFullYear(), Date.getMonth(), Date.getDate(), Date.getHours(), Date.getMinutes(), Date.getSeconds()
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20081210
 +
|author= [[User:MiGryz]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001229
 
}}
 
}}
 
==Overview==
 
 
This code snippet shows how to receive current system date and time using JavaScript's own methods.
 
  
 
==Source==
 
==Source==
  
Onto the HTML page, add the components into which the time will be printed:
+
Add the components where the time will be printed on the HTML page:
  
 
<code xml>
 
<code xml>
Line 25: Line 36:
 
</code>
 
</code>
  
The following functions handle the displaying of time:
+
The following functions handle displaying the time:
  
 
<code javascript>
 
<code javascript>
 
/**
 
/**
 
  * Displays the time in both standard (received from the system) and custom
 
  * Displays the time in both standard (received from the system) and custom
  * (built manually) formats.
+
  * (manually built) formats.
 
  */
 
  */
 
function displayTime() {
 
function displayTime() {
Line 73: Line 84:
 
</code>
 
</code>
  
Add also the call to <tt>setInterval</tt>, which in this case updates the time in every half seconds.
+
Also add the call to {{Icode|setInterval}} which in this case updates the time every 0.5 seconds.
  
 
<code javascript>
 
<code javascript>
Line 82: Line 93:
 
==Postconditions==
 
==Postconditions==
  
Time is displayed on the HTML page in both standard (received from the system) and custom (built manually) formats.
+
Time is displayed on the HTML page in both standard (received from the system) and custom (manually built) 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]]
+

Latest revision as of 09:31, 5 October 2012

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

Article Metadata
Code ExampleTested 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: MiGryz (10 Dec 2008)
Last edited: hamishwillee (05 Oct 2012)

[edit] 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();

[edit] Postconditions

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

[edit] 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.

This page was last modified on 5 October 2012, at 09:31.
67 page views in the last 30 days.
×