F
FAQ server
-----------------------------------------------------------------------
FAQ Topic - 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 = date.getFullYear(), sign = "", yyyy, mm, dd;
if(year < 0) {
sign = "-";
year = -year;
}
yyyy = sign + padLeft(year, 4, "0"),
mm = padLeft(date.getMonth() + 1, 2, "0"),
dd = padLeft(date.getDate(), 2, "0");
return yyyy + "-" + mm + "-" + dd;
}
/**
* @param {string} input: input value converted to string.
* @param {number} size: desired length of output.
* @param {string} ch: single character to prefix to s.
*/
function padLeft(input, size, ch) {
var s = input + "";
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 `.
(The ` T ` may be replaced with whitespace, where that
would not cause ambiguity).
http://jibbering.com/faq/#onlineResources
http://en.wikipedia.org/wiki/ISO_8601
http://isotc.iso.org/livelink/livelink/4021199/ISO_8601_2004_E.zip?func=doc.Fetch&nodeid=4021199
http://www.merlyn.demon.co.uk/js-date9.htm
The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/index.html.
FAQ Topic - 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 = date.getFullYear(), sign = "", yyyy, mm, dd;
if(year < 0) {
sign = "-";
year = -year;
}
yyyy = sign + padLeft(year, 4, "0"),
mm = padLeft(date.getMonth() + 1, 2, "0"),
dd = padLeft(date.getDate(), 2, "0");
return yyyy + "-" + mm + "-" + dd;
}
/**
* @param {string} input: input value converted to string.
* @param {number} size: desired length of output.
* @param {string} ch: single character to prefix to s.
*/
function padLeft(input, size, ch) {
var s = input + "";
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 `.
(The ` T ` may be replaced with whitespace, where that
would not cause ambiguity).
http://jibbering.com/faq/#onlineResources
http://en.wikipedia.org/wiki/ISO_8601
http://isotc.iso.org/livelink/livelink/4021199/ISO_8601_2004_E.zip?func=doc.Fetch&nodeid=4021199
http://www.merlyn.demon.co.uk/js-date9.htm
The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/index.html.