© J R Stockton, ≥ 2012-04-26

JavaScript Date and Time 9 :
Output Formatting
.

Links within this site :-

See "About This JavaScript Site" in JavaScript Index and Introduction.

See "General Date/Time Introduction" in JavaScript Date and Time Introduction.

String and Number Formatting

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 inc-cmmn.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.

Number to Two-Digit String

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.

Number to Three-Digit String

Day-of-Year fields should use three digits, for which LZZ is effective :-

This now gives optimal output for strange Numbers.

Adding Leading Zeroes into a String

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 :-

Date and Time Information

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 inc-date.js, can be seen in Include Files.

toJSON() for ISO 8601

2011-09-24 : My current browsers all include the toJSON method of the Date Object, which gives a string resembling ISO 8601 2009-02-13T23:31:30.123Z (the .123 may be absent). Note that it gives the GMT date and time, which are appropriate for an international medium like the Web. The following, however, will show the local date and time.

Default Output Formats

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.

Removing Separators

To remove the separators from a date/time string, for instance in converting 2000-02-29 to 20000229.htm, consider these :-

Format Caveats

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.

Date Information

See also Using ECMAScript 5th Edition.

Date Format

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.

Determining Numeric Output Field Order

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.

Date Object to Output Formats

To present a date in a preferred ISO 8601 numeric form, I have, in inc-date.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.

Suffixes

Suffixing - converting "1" to "1st" - is not always date-specific; see in Miscellany 1.

Numeric to Alphanumeric

To convert yyyy-mm-dd to dd-MMM-yyyy, consider :-

Conversion of Order

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 :-

Filename Creation

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.

Two-Digit Years

One can use LZ(Year%100) for this, or String(Year).substring(2) currently.

Time Information

To present a time in preferred full numeric form, as in ISO 8601, I have, in inc-date.js, added the method ISOlocaltimeStr (among others) to my Date Objects :-

Time Conversions

Watch out for localisation dependencies. The 12-hr clock is not appropriate for international use.

24-hr Time to Seconds-of-Day

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.

Seconds-of-Day to 24-hr Time

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.

Hours to HHhMMmSSs

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.

Interconverting 12-hr and 24-hr Time

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 inc-date.js, added the method USlocaltimeStr (among others) to my Date Objects :-

In H24to12 the simpler H24>11 normally suffices.

Time of Local Day, ms & h

Showing an Event in Local Civil Time

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.

The Third Millennium began at 2001-01-01 00:00 UTC in the place set in your system.
2008-05-01 18:07 UTCmore
2008-06-01 18:07 EDTdata
2008-07-01 18:07 PDTgoes
2008-08-01 18:07 badhere
Press Once

In earlier Opera, new Date(String) ignored all alphabetic offset designators apart from GMT & UTC. Therefore, only the first button-press would have a visible effect.

JavaScript Date and Time Index
Home Page
Mail: no HTML
© Dr J R Stockton, near London, UK.
All Rights Reserved.
These pages are tested mainly with Firefox and W3's Tidy.
This site, , is maintained by me.
Head.