"Split & Parts" different results in Firefox & IExplorer

K

kinne

The following code is supposed to reverse the date in "yyyy-mm-dd" format,
but it produces different results in Firefox 1.0 and in Internet Explorer
6SP1. In Firefox, the result is correct ("2004-11-29") but it's wrong in
Internet Explorer 6SP1 ("00:20:15-11-29"). If I change "dateParts[3]" to
"dateParts[4]", it's exactly the opposite that occures: a correct result in
IExplorer but a fault in Firefox. Is there a workaround? Where do I miss the
point??
function SplitDate()
{
// Split current date.
today = Date();
dateParts = today.split(" ");

// Assign splitted date parts to variables.
year=dateParts[3];
month=GetMonthNumber(dateParts[1]);
day=dateParts[2];

// Build date in "yyyy-mm-dd" format
dateStamp = year + "-" + month + "-" + day;
}
Result in Firefox 1.0 is => 2004-11-29
Result in Internet Explorer 6 => 00:20:15-11-29

Kinne
 
R

RobG

kinne said:
The following code is supposed to reverse the date in "yyyy-mm-dd" format,
but it produces different results in Firefox 1.0 and in Internet Explorer
6SP1. In Firefox, the result is correct ("2004-11-29") but it's wrong in
Internet Explorer 6SP1 ("00:20:15-11-29"). If I change "dateParts[3]" to
"dateParts[4]", it's exactly the opposite that occures: a correct result in
IExplorer but a fault in Firefox. Is there a workaround? Where do I miss the
point??

You are depending upon the browser's default date format to fit your
script - not a good idea, particularly when you can access the parts of
the date directly and hence, reliably.

Try this:

<script type="text/javascript">
function iso8601date(){
var aDate = new Date();
var dateNum = aDate.getDate();
var monthNum = +aDate.getMonth() + 1; // month range is 0-11
var yearNum = aDate.getFullYear();
alert('date is ' + yearNum
+ '-' + LZ(monthNum)
+ '-' + LZ(dateNum)
);
}

function LZ(x) { return (x<0||x>=10?"":"0") + x }
</script>

You may want not want to add leading zeros to single digit months and
days - it isn't necessary for ISO 8601 compliance but some think it
looks better.

LZ() courtesy of an earlier post by Dr John Stockton.

If you are going to use dates input by the user, then you must do a lot
of validation on the string that is input and on date ranges -
different browsers support different ranges (e.g. Safari has a range of
1900 to 2038, other browsers have much greater ranges) so you must
validate that the date created from the input is a valid date.

Search for date posts, you will find plenty of good advice.
 
D

Dr John Stockton

JRS: In article <f%[email protected]>, dated Mon, 29
Nov 2004 00:24:43, seen in RobG
function LZ(x) { return (x<0||x>=10?"":"0") + x }
</script>

You may want not want to add leading zeros to single digit months and
days - it isn't necessary for ISO 8601 compliance but some think it
looks better.


<BIG><BIG><BIG> HO YES IT IS NECESSARY </BIG></BIG></BIG>

ISO8601:2000(E) 5.2.1 is clear; and probably so for other values of E.

The year, except by agreement, should also be four digits (more after
9999).

In Y-D dates, D must be 3 digits.

Unless field lengths are constant, and separators do not fluctuate, the
full advantages of 8601 do not accrue - fixed-length dates can be sorted
as strings, fields can be extracted by position, ...

I predict an update, ISO8601:9???, making 5-digit years mandatory.
 
R

RobG

Dr John Stockton wrote:
[...]
<BIG><BIG><BIG> HO YES IT IS NECESSARY </BIG></BIG></BIG>

ISO8601:2000(E) 5.2.1 is clear; and probably so for other values of E.

Yes, quite correct. I can't think where I go the idea from. I've been
working extensively with metadata standards and absolutely should have
known - I can only think some brain-bits got flipped the wrong way.

[...]
I predict an update, ISO8601:9???, making 5-digit years mandatory.

And you owe me a beer if it hasn't happened by 9999-12-31.
 
K

Kinne

You are depending upon the browser's default date format to fit your
script - not a good idea, particularly when you can access the parts of
the date directly and hence, reliably.

Try this:
[snip]


Thanks a lot: your solution works fine in both browsers.

François
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top