Scott Sauyet wrote:
> Laser Lips wrote:
>> would expect JavaScript to use it's local settings and use Gennaio
>> instead of January, but it does not. It appears to continue to use
>> English Dates.
>> So my Italians are putting in things like CompareTwoDates("01 Gennaio
>> 2009","01 Gennaio 2010",">") but the function is failing because
>> javascript does not understand the month Gennaio.
>>
>> How can I tell JavaScript to use another language?
>
> I may be mistaken, but I don't think you can tell Javascript that. I
> think your function will need to do more work.
And the function needs a lot of work, too.
> You might start by adding
>
> CompareTwoDates.monthNames = [
The function identifier really should not start uppercase here.
> "January", "February", "March", "April", "May", "June",
> "July", "August", "September", "October", "November",
> "December"];
>
> and then using the index of the month name in this array as part of
> your algorithm. (I don't know if you also need "Jan", "Feb", "Mar",
> etc. or not, but they would be easy to add.)
A possibility, but consider the implications. For mapping strings
to numbers is generally more efficient to use an Object instance:
CompareTwoDates.monthNames = {
"January": 0,
"February": 1,
"March": 2,
"April": 3,
"May": 4,
"June": 5,
...
};
Since it is rather tedious and potentially error-prone to do the counting
manually, the following loop can use the mapping nature inherent to all
ECMAScript objects to reuse your array for a map (just make sure no month
is named like a built-in property of Array instances; see also my Map
implementation):
for (var a = CompareTwoDates.monthNames,
i = a.length;
i--

{
a[a[i]] = i;
}
Afterwards a["January"] (or a.January) would result in 0, a["February"] in
1 aso.
(It becomes a bit more complex if you also want to support abbreviations.)
The number can then be passed as second argument to the Date constructor in
order to have date values for the comparison.
However, it would be better if CompareTwoDates (better: compareTwoDates)
would also accept references to two Date instances, and instead of using
the third string argument returned a numeric value `r' indicating the
relation between the two (computed) date values (as it is customary: r < 0
if date1 < date2, r === 0 if date1 == date2, and r > 0 if date1 > date2).
Another misuse of eval() could be avoided that way.
> Then if it's running somewhere with a different language, you can just
> override this array.
ACK
> But I wonder if you've thought this through entirely. What happens if
> run in the U.S., where traditional date formats are more like "January
> 6, 2010"?
True. However, if there is always a month name or abbreviation, and the
year format is Y2K-safe (i.e., more than two digits), a Regular Expression
can handle the matching.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16