Connecting Tech Pros Worldwide Forums | Help | Site Map

"Split & Parts" different results in Firefox & IExplorer

kinne
Guest
 
Posts: n/a
#1: Jul 23 '05
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??
[color=blue][color=green]
>>[/color][/color]
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;
}
[color=blue][color=green]
>>[/color][/color]
Result in Firefox 1.0 is => 2004-11-29
Result in Internet Explorer 6 => 00:20:15-11-29

Kinne



RobG
Guest
 
Posts: n/a
#2: Jul 23 '05

re: "Split & Parts" different results in Firefox & IExplorer


kinne wrote:[color=blue]
> 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??
>[/color]

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.

--
Rob
Dr John Stockton
Guest
 
Posts: n/a
#3: Jul 23 '05

re: "Split & Parts" different results in Firefox & IExplorer


JRS: In article <f%tqd.889$I52.36815@news.optus.net.au>, dated Mon, 29
Nov 2004 00:24:43, seen in news:comp.lang.javascript, RobG
<rgqld@iinet.net.auau> posted :[color=blue]
>
> 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.[/color]


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

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
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 moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
RobG
Guest
 
Posts: n/a
#4: Jul 23 '05

re: "Split & Parts" different results in Firefox & IExplorer


Dr John Stockton wrote:
[...][color=blue]
>
> <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.
>[/color]

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.

[...][color=blue]
> I predict an update, ISO8601:9???, making 5-digit years mandatory.
>[/color]

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

--
Rob
Kinne
Guest
 
Posts: n/a
#5: Jul 23 '05

re: "Split & Parts" different results in Firefox & IExplorer


> You are depending upon the browser's default date format to fit your[color=blue]
> script - not a good idea, particularly when you can access the parts of
> the date directly and hence, reliably.
>
> Try this:[/color]
[snip]


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

François
Closed Thread