Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Javascript function for pretty date formatting using PHP format strings

4 views
Skip to first unread message

Jason

unread,
Nov 19, 2009, 5:28:40 PM11/19/09
to
We created a simple Javascript function for date formatting to use on
FeedMagnet.com (a tool that helps businesses harness social media).
We're releasing the code to the public and hope other can benefit from
it. It essentially maps the PHP date formatting character strings
(e.g. "F j, Y") so you can use them in Javascript.

Here's the link for more explanation and the code: http://budurl.com/jsdate

- Jason Ford, FeedMagnet founder

Thomas 'PointedEars' Lahn

unread,
Nov 19, 2009, 5:40:42 PM11/19/09
to
Jason wrote:

Looks unnecessarily complicated and inefficient. Try
String.prototype.replace(RegExp, Function) next time.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

Dr J R Stockton

unread,
Nov 20, 2009, 4:10:17 PM11/20/09
to
In comp.lang.javascript message <96661402-8d83-4437-b2c7-f8a55696811f@v3
0g2000yqm.googlegroups.com>, Thu, 19 Nov 2009 14:28:40, Jason
<in...@feedmagnet.com> posted:

Have you considered testing the code?

Near the end, I see a line

var m = date.getHours() > 0 && date.getHours < 12 ? 'am' : 'pm'

so it appears at first that 'am' is used from 01:00 up to 12:00 and 'pm'
is used for 00:00 up to 01:00 and 12:00 up to 24:00. But perhaps
reading the rest of the code would explain it. There again, the lack of
parentheses after the second getHours is interesting; it eliminates
mornings.

There's an intrinsic problem with the 12-hour clock for times which
cannot by general context be placed within a range of at most 12 hours.
To want to use it, one must be daft; to use it correctly, one must not
be daft. Fortunately, over 99% of countries seem to understand ISO 8601
well enough to be able to use it within IT work.

That page needs inspection by its author in Firefox with text zoom. I
have to zoom out to see the ends of the code lines; but then the entire
code is too small and faint to read.

Most of the Date methods, especially the non-UTC ones, are relatively
slow. Therefore, where a result is used more than once (e.g.
getHours()), it should be determined once and saved for following use.

Compare, for example, the line (wrapped here)

if (format.indexOf('D') > -1 || format.indexOf('N') > -1 ||
format.indexOf('w') > -1 || format.indexOf('l') > -1) {

with

if ( /[DNwl]/.test(format) ) {

which is certainly shorter and is somewhat quicker in Firefox 3.0.15.


You should have used the Wayback machine to read old versions of the
FAQ on the subject of date.

The FeedMagnet site gives no clear indication of who is responsible for
it, or what country it comes from. Reputable authors, and many others,
always give a clear indication of who is responsible for the work.
Without that, one naturally assumes the worst. The site and code seem
to justify that - for example, I see a price of "$50/year/employee"
without any indication of which breed of $ is intended.

On the cited page, http://validator.w3.org/ gives '3 Errors, 1
warning(s)'. That seems hopeful, until one sees ' Line 19, Column 31:
script element between head and body. <script
type="text/javascript">Line 19, Column 31: Cannot recover after last
error. Any further errors will be ignored.
<script type="text/javascript">' and that it has 270 more lines.

I suppose, therefore, that you don't validate your pages.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

Thomas 'PointedEars' Lahn

unread,
Nov 21, 2009, 12:14:22 AM11/21/09
to
Dr J R Stockton wrote:

> Most of the Date methods, especially the non-UTC ones, are relatively
> slow. Therefore, where a result is used more than once (e.g.
> getHours()), it should be determined once and saved for following use.
>
> Compare, for example, the line (wrapped here)
>
> if (format.indexOf('D') > -1 || format.indexOf('N') > -1 ||
> format.indexOf('w') > -1 || format.indexOf('l') > -1) {
>
> with
>
> if ( /[DNwl]/.test(format) ) {
>
> which is certainly shorter and is somewhat quicker in Firefox 3.0.15.

You are correct, of course (this is part of what I was referring to).
However, the Firefox 3.0.x branch dies by the end of 2010-01 CE. In
general, I do not think it it is wise to optimize for or test with user
agents which are known to meet their end-of-life within the next three
months. YMMV.

PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300...@news.demon.co.uk> (2004)

Dr J R Stockton

unread,
Nov 21, 2009, 6:05:43 PM11/21/09
to
In comp.lang.javascript message <1509814.n...@PointedEars.de>,
Sat, 21 Nov 2009 06:14:22, Thomas 'PointedEars' Lahn
<Point...@web.de> posted:

>Dr J R Stockton wrote:
>
>> Most of the Date methods, especially the non-UTC ones, are relatively
>> slow. Therefore, where a result is used more than once (e.g.
>> getHours()), it should be determined once and saved for following use.
>>
>> Compare, for example, the line (wrapped here)
>>
>> if (format.indexOf('D') > -1 || format.indexOf('N') > -1 ||
>> format.indexOf('w') > -1 || format.indexOf('l') > -1) {
>>
>> with
>>
>> if ( /[DNwl]/.test(format) ) {
>>
>> which is certainly shorter and is somewhat quicker in Firefox 3.0.15.
>
>You are correct, of course (this is part of what I was referring to).
>However, the Firefox 3.0.x branch dies by the end of 2010-01 CE. In
>general, I do not think it it is wise to optimize for or test with user
>agents which are known to meet their end-of-life within the next three
>months. YMMV.

A rather silly remark. I gave 3.0.15 as a statement of fact; it was
what I tested with.

It is marginally faster in Chrome 3.0.195.33, Safari 4.0.3, Opera 10.01,
and considerably slower in MSIE 8 (but marginally faster there with
RegExp defined outside my test loop, which also helps Safari).

But the only important point is that the speed is not much worse; the
differences will not affect the perceived speed in real use.


Don't confuse end-of-life with what the manufacturer says; end-of-life
is determined by the users. Firefox 3.5 is, I read, unsatisfactory to
many users.

--
(c) John Stockton, Surrey, 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 "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

0 new messages