comp.lang.javascript FAQ - Quick Answers 2009-03-02

comp.lang.javascript FAQ - Quick Answers 2009-03-02

Post by FAQ serve » Tue, 03 Mar 2009 18:00:02


omp.lang.javascript FAQ - Quick Answers- 11, Updated January 26, 2009

1 Meta-FAQ meta-questions
1.1 _Which newsgroups deal with javascript_?
1.2 _What questions are on-topic for comp.lang.javascript_?
1.3 _What should I do before posting to comp.lang.javascript_?
1.4 _Why was my post not answered_?

You are reading the _comp.lang.javascript_ meta-FAQ,
version 11. It is available on the web at
<URL: http://www.jibbering.com/faq/index.html > in HTML form.

This FAQ provides URLs to further information about ECMAScript
(loosely called javascript), and some hints and tips to make your
stay in comp.lang.javascript more enjoyable.

This document was created for three reasons: to help reduce the
high levels of noise on c.l.js, to provide a resource for people
new to javascript, and to point to locations that contain
valuable javascript resources.

Each day, one section of the FAQ is posted for review and questions,
and as a reminder that the FAQ is available.

For additional explanation and detail relating to some aspects
of the FAQ, please see the
FAQ Notes <URL: http://www.jibbering.com/faq/faq_notes/faq_notes.html >.
It has been provided separately to avoid increasing the size of
the FAQ to a point where it would be unreasonable to post it to
the group.

2 Language Overview
2.1 _What is ECMAScript_?
2.2 _What is JScript_?
2.3 _What are object models_?
2.4 _What is the document object model_?
2.5 _Internationalisation and Multinationalisation in javascript.
2.6 _What does the future hold for ECMAScript_?

3 Javascript Resources
3.1 _What books are recommended for javascript_?
3.2 _What online resources are available_?

4 Dates
4.1 _How do I format a date with javascript_?

A date can be formatted to a common ISO 8601 format with:-


/** Get IS0 8601 format YYYY-MM-DD from a Date Object */
function formatDate(date){
var year = padString(date.getFullYear() + "", 4, "0"),
month = padString(date.getMonth() + 1 + "", 2, "0"),
dayOfMonth = padString(date.getDate() + "", 2, "0");
return year + "-" + month + "-" + dayOfMonth;
}

/**
* @param {string} s : input to prefixed.
* @param {number} size : desired length of output.
* @param {string} ch : single character to prefix to s.
*/
function padString(s, size, ch) {
while (s.length < size) {
s = ch + s;
}
return s;
}



Never use a local date/time for a non-local event. Instead, use UTC,
as in ISO 8601 YYYY-MM-DDThh:mm:ssZ (Z is the only letter suffix).

For a local date/time with time offset, to unambiguously indicate a
particular instant, use ISO 8601 format YYYY-MM-DDThh:mm:ss±hh:mm.

<URL: http://jibbering.com/faq/#onlineResources >
<URL: http://en.wikipedia.org/wiki/ISO_8601 >
<URL: http://isotc.iso.org/livelink/livelink/4021199/ISO_8601_2004_E.zip?func=doc.Fetch&nodeid=4021199 >
<URL: http://www.merlyn.demon.co.uk/js-date9.htm >



5 Numbers
5.1 _How do I format a Number as a String with exactly 2 decimal places_?

When formatting money for example, to format 6.57634 to
6.58, 6.5 to 6.50, and 6 to 6.00?

Rounding of x.xx5 is uncertain, as such numbers are not
represented exactly. See also:
Why does simple decimal arithmetic give strange results? <URL: #binaryNumbers >n = Math.round( n * 100)/100 converts n to a Numb