Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > FAQ Topic - How do I format a Date object with javascript? (2010-07-26)

Reply
Thread Tools

FAQ Topic - How do I format a Date object with javascript? (2010-07-26)

 
 
Dr J R Stockton
Guest
Posts: n/a
 
      08-05-2010
In comp.lang.javascript message <(E-Mail Removed)>, Wed, 4 Aug
2010 18:16:03, Lasse Reichstein Nielsen <(E-Mail Removed)> posted:

>dhtml <(E-Mail Removed)> writes:
>
>> On Jul 30, 7:45 am, Asen Bozhilov <(E-Mail Removed)> wrote:

>
>>> return (yyyy + "-" + mm + "-" + dd).replace(/\b1/g, '');

>
>I think RegExp replace is a little heavy-duty for something like this.
>How about
> return String(yyyy).substring(1) + "-" +
> String(mm).substring(1) + "-" +
> String(dd).substring(1);
>?


One should not expect the date formatter to be called in isolation. The
proper way must IMHO be to have a rather general padding function or a
set of moderately general ones, such as

function LZ(n) { return (n!=null&&n<10&&n>=0?"0":"") + n }

function SpcsTo(S, L) { S += ""
while (S.length<L) S = " " + S ; return S }

function ZeroTo(S, L) { S += ""
while (S.length<L) S = "0" + S ; return S }

function ToNd(X, N) { // developed from ZeroTo
if (X<0) return "-" + ToNd(-X, N)
X += "" ; while (X.length<N) X = "0" + X ; return X }

function ChrsTo(S, L, C) {
for (var i = String(S).length ; i < L ; i++) S = C + S
return S }

function TailTo(X, Ch, L) { var S = String(X)
while (S.length < L) S += Ch
return S }

(or, if preferred, Methods of Number) and then to write something like

return ZeroTo(yyyy, 4) + "-" + LZ(mm) + "-" + LZ(dd)

Note that LZ gives an appropriate result for any possible value of n
including NaN (except for those who want to see '-0').

Now there is for example an obvious way (for almost all continents) of
formatting time with LZ; and formatting ww in standard Week Numbers, and
ddd in Ordinal Dates and integer degrees or grads (though I also have a
LZZ for that),

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ Topic - How do I format the Last-Modified date with javascript? FAQ server Javascript 0 03-09-2007 12:00 AM
FAQ Topic - How do I format Last Modified date with javascript? FAQ server Javascript 0 01-06-2007 12:00 AM
FAQ Topic - How do I format Last Modified date with javascript FAQ server Javascript 1 11-09-2006 08:10 PM
FAQ Topic - How do I format Last Modified date with javascript FAQ server Javascript 0 09-11-2006 11:00 PM



Advertisments