Javascript function for pretty date formatting using PHP formatstrings

J

Jason

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
 
T

Thomas 'PointedEars' Lahn

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

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


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <96661402-8d83-4437-b2c7-f8a55696811f@v3
0g2000yqm.googlegroups.com>, Thu, 19 Nov 2009 14:28:40, Jason
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

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.
 
T

Thomas 'PointedEars' Lahn

Dr said:
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
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
Sat, 21 Nov 2009 06:14:22, Thomas 'PointedEars' Lahn
Dr said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top