make a date pretty

Q

quillbreaker

I'd like a Javascript snippet that converts a dot.net style date
string (say, 9/1/2006) into the pretty version (September 1st, 2006),
so my users get a visual cue when they are entering January 2nd,2009
when they meant to enter Febuary 1st, 2009. Does anyone have such a
thing lying around somewhere? I'm not much of a javascript programmer.
 
T

Thomas 'PointedEars' Lahn

I'd like a Javascript snippet that converts a dot.net style date
string (say, 9/1/2006) into the pretty version (September 1st, 2006),
so my users get a visual cue when they are entering January 2nd,2009
when they meant to enter Febuary 1st, 2009. Does anyone have such a
thing lying around somewhere?

Yes, it's in the archives. Using Google Groups already, you will have no
difficulty at all to find them.
I'm not much of a javascript programmer.

That's tough luck, though.


PointedEars
 
R

RobG

I'd like a Javascript snippet that converts a dot.net style date
string (say,  9/1/2006) into the pretty version (September 1st, 2006),
so my users get a visual cue when they are entering January 2nd,2009
when they meant to enter Febuary 1st, 2009.  Does anyone have such a
thing lying around somewhere?  I'm not much of a javascript programmer.

If you don't know much about javascript, you wont know good code from
bad so spend a bit of time learning the basics. You can probably find
hundreds of scripts and libraries that will do what you want, but
writing it yourself isn't that tough.

Below is an example - not production ready but should start you on the
right track:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Enter Date</title>
<style type="text/css">
body {
font-family: geneva, arial, sans-serif;
}
td {
vertical-align: top;
}
#dateA_full {
font-family: arial, sans-serif;
font-size: 80%;
color: #666666;
background-color: ffffff;
}

</style>
<script type="text/javascript">

// Expects date, month, year
function validateDate(d, m, y) {
var D = new Date( y + '/' + m + '/' + d);
return d == D.getDate() && m == (D.getMonth()+1);
}

// Adds 'st', 'nd', etc. to numbers
function addOrdinal(n) {
n = n % 100;
var s = ["th", "st", "nd", "rd", "th"];
var ord = (n<21)? ((n < 4)? s[n] : s[0])
: ((n%10 > 4)? s[0] : s[n%10]);
return n + ord;
}

// Expects a date as dd/mm/yyy, returns as date with ordinal
// month as word and year, e.g.
// 1/2/2008 -> 1st February, 2008
function formatDate(txt) {
var months = ['','January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December'];
var dateBits = txt.split('/');

if (dateBits.length == 3 &&
validateDate(dateBits[0], dateBits[1], dateBits[2]))
{
return addOrdinal(dateBits[0]) + ' ' +
months[dateBits[1]] + ', ' +
dateBits[2];
} else {
return 'Doesn\'t seem to be a valid date&hellip;';
}
}

</script>
</head>
<body>
<table>
<tr>
<td>Enter date (dd/mm/yyyy):
<td><input type="text" name="dateA" onblur="
document.getElementById('dateA_full').innerHTML =
formatDate(this.value);
">
<br>
<span id="dateA_full">&nbsp;</span>
</table>

</body>
</html>
 
D

Dr J R Stockton

In comp.lang.javascript message <ec8a5447-6935-4bac-8482-7dee900ad638@26
g2000hsk.googlegroups.com>, Thu, 2 Oct 2008 14:37:51,
(e-mail address removed) posted:
I'd like a Javascript snippet that converts a dot.net style date
string (say, 9/1/2006) into the pretty version (September 1st, 2006),
so my users get a visual cue when they are entering January 2nd,2009
when they meant to enter Febuary 1st, 2009. Does anyone have such a
thing lying around somewhere? I'm not much of a javascript programmer.

If the previous FAQ maintainer had not removed the line, you would have
found adequate guidance in the newsgroup FAQ, if you had been wise
enough to read it before posting. As is, you would have had to perceive
an implicit clue.

FFF (and BRF) dates should not be used on the Web, which is an
international medium. Instead, follow ISO 8601. The error you describe
is unlikely to occur when date fields are in order of descending
significance.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
D

dhtml

Dr said:
In comp.lang.javascript message <ec8a5447-6935-4bac-8482-7dee900ad638@26
g2000hsk.googlegroups.com>, Thu, 2 Oct 2008 14:37:51,
(e-mail address removed) posted:

If you are using .Net, do it on the server. Search for a l10n aware
dateformatter in the language you're using.
If the previous FAQ maintainer had not removed the line, you would have
found adequate guidance in the newsgroup FAQ, if you had been wise
enough to read it before posting. As is, you would have had to perceive
an implicit clue.

FFF (and BRF) dates should not be used on the Web, which is an
international medium. Instead, follow ISO 8601. The error you describe
is unlikely to occur when date fields are in order of descending
significance.

Could also go here:

2.10 Internationalization and Multinationalization in javascript.


| For example, there is an International Standard
| for numeric Gregorian date format; but none for decimal
| and thousands separators.

There is a separator defined in CLDR. If you want to L10N on the client,
for dates, currencies, numbers, look into CLDR. I can't at the moment
bring up unicode.org site, but it is there.

The same thing goes for dates.

Garrett
 
D

Dr J R Stockton

In comp.lang.javascript message <7cdd2227-7218-483b-a89d-13da62683a02@x4
1g2000hsb.googlegroups.com>, Thu, 2 Oct 2008 19:11:59, RobG
return d == D.getDate() && m == (D.getMonth()+1);
var dateBits = txt.split('/');

If a RegExp match is used instead of Array split, and if that match
allows at most two digits for day-of-month, then only the Month equality
test will be needed.

Your code fails in Century Zero (no sense of history in the ex-
Colonies?) <g>.
 
D

Dr J R Stockton

  2.10 Internationalization and Multinationalization in javascript.

|  For example, there is an International Standard
| for numeric Gregorian date format; but none for decimal
| and thousands separators.

There is a separator defined in CLDR. If you want to L10N on the client,
for dates, currencies, numbers, look into CLDR. I can't at the moment
bring up unicode.org site, but it is there.

The same thing goes for dates.

Whatever CLDR means, it is not an International Standard unless it has
been published by ISO as such.

To get an International Standard for decimal and thousands separators,
it would at present be necessary for the French and the Americans to
agree on which of them should change. IMHO, that will not happen,
until enforced by the Chinese.
 
J

John W Kennedy

Dr said:
Whatever CLDR means, it is not an International Standard unless it has
been published by ISO as such.

The Unicode Common Locale Data Repository. It's not a standard for
thousands separators (an impossibility at present, as you observe
below), but, as the name indicates, is planned to be a good place, when
it is released, to obtain the correct value.
To get an International Standard for decimal and thousands separators,
it would at present be necessary for the French and the Americans to
agree on which of them should change. IMHO, that will not happen,
until enforced by the Chinese.

--
John W. Kennedy
"Give up vows and dogmas, and fixed things, and you may grow like
That. ...you may come to think a blow bad, because it hurts, and not
because it humiliates. You may come to think murder wrong, because it
is violent, and not because it is unjust."
-- G. K. Chesterton. "The Ball and the Cross"
 
R

RobG

In comp.lang.javascript message <7cdd2227-7218-483b-a89d-13da62683a02@x4
1g2000hsb.googlegroups.com>, Thu, 2 Oct 2008 19:11:59, RobG


If a RegExp match is used instead of Array split, and if that match
allows at most two digits for day-of-month, then only the Month equality
test will be needed.

Sure, but I don't think it provides any benefit. The RegExp can be
build once to save some processing power, but you are still trading a
RegExp comparison with a getMonth comparison. Calling it several
thousand times in succession may prove the point but I think it's
moot.

Your code fails in Century Zero (no sense of history in the ex-
Colonies?) <g>.

Some of us claim to have been here for 60,000 years or so - I'm not
sure the Gregorian calendar has much meaning for that timeframe, "the
dreaming" seems far more appropriate. ;-)
 
D

dhtml

John said:
The Unicode Common Locale Data Repository. It's not a standard for
thousands separators (an impossibility at present, as you observe
below), but, as the name indicates, is planned to be a good place, when
it is released, to obtain the correct value.

I'm not that versed in unicode standards -- far from being an expert,
but here's what I have read and what I understand from it:-

Grouping separator is apparently localized. Not a "standardized" symbol.
It varies between locales.

| For example, the decimal separator set could include all of [.,']

http://unicode.org/reports/tr35/tr35-7.html#Number_Format_Patterns


| G.1 Number Patterns
|
| The NumberElements resource affects how these patterns are interpreted
| in a localized context. Here are some examples, based on the French
| locale. The "." shows where the decimal point should go. The "," shows
| where the thousands separator should go. A "0" indicates zero-padding:
| if the number is too short, a zero (in the locale's numeric set) will
| go there. A "#" indicates no padding: if the number is too short,
| nothing goes there. A "¤" shows where the currency sign will go. The
| following illustrates the effects of different patterns for the French
| locale, with the number "1234.567". Notice how the pattern characters
| ',' and '.' are replaced by the characters appropriate for the locale.

The last two lines state that the "pattern characters" are "replaced by
the characters appropriate for the locale."


I think the point is that the separator varies, but that variance is
taken on by CLDR.

Garrett
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top