See "About This JavaScript Site" in JavaScript Index and Introduction.
See "General Date/Time Introduction" in JavaScript Date and Time Introduction.
Note that browsers vary and new versions have changes. To be truly safe, avoid the built-in direct conversions between Date Object and String by using intermediate Numbers.
Some string and number formatting methods, coded in include1.js, can be seen in Include Files.
String reformatting operations may be of use both with input and with output.
In some browsers, the default conversion from Date can give a surprisingly long String.
Note that, unlike some other approaches, my Leading-Zero functions always return type String; this is a safety feature.
Most date/time numeric fields should use two digits, for which these functions are effective for in-range values :-
It is here assumed that the argument will necessarily be of type Number (except that LS can be used with an Array of Number).
P4/3G XP sp2 IE6, 4s — of the good ones, LZ wins.
Day-of-Year fields should use three digits, for which LZZ is effective :-
This now gives optimal output for strange Numbers.
Each of these adds a leading zero to any single digit between word boundaries, to expand existing strings with single-digit fields where two digits are preferred :-
For description of Written Date and Time Formats, see 0: Date Object Information and Date and Time Formats.
Various date/time formatting methods, coded in include3.js, can be seen in Include Files.
The button shows some default output formats of your browser, for various years.
Browser output formats should not be relied upon; if it matters, compose the string from numbers. There may be no default method always giving a separated-numeric date form such as 01/02/03.
In MS IE 6, for example, the format of toLocaleString is governed by Windows Control Panel for dates in 1601-9999 AD.
To remove the separators from a date/time string, for instance in converting 2000-02-29 to 20000229.htm, consider these :-
In news:microsoft.public.scripting.jscript, Peter Torr wrote on
2001-11-10, referring to MSIE :-
Date.prototype.toLocaleString changed behaviour from 5.0 to 5.5.
It used to always return the date in US format, regardless of your
locale (not exactly a "locale-sensitive string"). It now returns the
date in the Long Date format as specified in Control Panel options.
There may be unexpected changes of format with date in some systems, particularly JScript.
At least in MS IE 6 and JScript.NET, the format of Date.toLocaleString is that of Control Panel only for years 1601-9999; otherwise it is that of .toString.
Formats given by JavaScript can depend not only on browser version but also on patch level.
See also Using ECMAScript 5th Edition.
The UK and Europe use (with varying separators) dd/mm/yyyy; the USA uses mm/dd/yyyy; but localisation of a browser may be incorrect. This must be allowed for in using date strings on Web pages.
The ISO 8601 basic format is YYYY-MM-DD, which is unambiguous everywhere.
This tests the output format, assuming that toLocaleString() gives a numeric format, YMD DMY or MDY. That was true in IE 4, but is not so in IE 6. The format can, but need not, be given by OS settings.
To present a date in a preferred ISO 8601 numeric form, I have, in include3.js, added the method ISOlocaldateStr (among others) to my Date Objects :-
To get the current local date as YYYYMMDD or YYYYMMDDhhmmss :-
To get the current local date as YYYYMMDD, by RegExp :-
To present the current date as yyyy-MMM-dd; from which a function(Y, M, D) can be deduced :-
For Week Numbering dates, see in Working with Weeks.
Suffixing - converting "1" to "1st" - is not always date-specific; see in Miscellany 1.
To convert yyyy-mm-dd to dd-MMM-yyyy, consider :-
Dates can be compared as strings, if in ISO 8601 order with fields YYYY, MM, DD; the customary UK notation dd/mm/yyyy can be converted to that with UKtoISO8601 and the customary US order mm/dd/yyyy can be converted with UStoISO8601 or otherwise. See above for Leading Zeroes.
In any case, field lengths may need to be regularised.
Use ISO 8601 order whenever possible, especially when there may be later processing.
This RegExp converter from US order to UK order works equally well backwards, alas :-
For a fixed-format initial date string :-
IS = "21/02/2345" FN = IS.replace(/(..).(..).(....)/, "Prelude$3-$2-$1postlude.extn") // gives Prelude2345-02-21postlude.extn
Generally, when a date is used in a name, using the logical YMD order and fixed field-width is advantageous.
One can use LZ(Year%100) for this, or String(Year).substring(2) currently.
To present a time in preferred full numeric form, as in ISO 8601, I have, in include3.js, added the method ISOlocaltimeStr (among others) to my Date Objects :-
Watch out for localisation dependencies. The 12-hr clock is not appropriate for international use.
To convert a well-formed time to seconds-of-day, in two ways, ignoring Summer Time changes, try as in :-
which assume that the String will contain hh:mm:ss. Note the difference for out-of-range input.
Ignoring Summer Time change days, to convert seconds-of-day to h,m,s numbers, and (in three ways) to a well-formed time, try as in :-
Note the difference for input outside 0..86399. T3 and T4 should be tested in a location that does not use GMT in January.
The first method uses date-to-string conversion in a manner probably not locale-dependent; the other two use plain arithmetic. The methods can be adapted for similar purposes.
For validation, see Date and Time 4 : Validation; for principles, see in Date and Time Formats.
To present a time in American 12-hr form, I have, in include3.js, added the method USlocaltimeStr (among others) to my Date Objects :-
In H24to12 the simpler H24>11 normally suffices.
A page can have absolute date/times (i.e. with offset indications) in the source, but by using JavaScript can display them as user's Local Civil Time (LCT). One can use <body onLoad="ShowLCT('Target', 'Class')"> to replace Class fields within the element with ID Target. Users without JavaScript will see the original data.
2008-05-01 18:07 UTC | more |
2008-06-01 18:07 EDT | data |
2008-07-01 18:07 PDT | goes |
2008-08-01 18:07 bad | here |
Press Once |
---|
In Opera, new Date(String) ignores all alphabetic offset designators apart from GMT & UTC. Therefore, only the first button-press has a visible effect.