Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / November 2004



Tip: Looking for answers? Try searching our database.

"Split & Parts" different results in Firefox & IExplorer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kinne - 28 Nov 2004 23:52 GMT
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
RobG - 29 Nov 2004 00:24 GMT
> 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
[quoted text clipped - 3 lines]
> 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.

Signature

Rob

Dr John Stockton - 29 Nov 2004 14:11 GMT
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 :

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

Signature

© 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 - 29 Nov 2004 22:49 GMT
[...]

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

Signature

Rob

Kinne - 30 Nov 2004 22:40 GMT
>   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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.