473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

"Split & Parts" different results in Firefox & IExplorer

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
Jul 23 '05 #1
4 2394
kinne wrote:
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.

--
Rob
Jul 23 '05 #2
JRS: In article <f%*****************@news.optus.net.au>, dated Mon, 29
Nov 2004 00:24:43, seen in news:comp.lang.javascript, RobG
<rg***@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.

--
© 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.
Jul 23 '05 #3
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.

--
Rob
Jul 23 '05 #4
> 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
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
11
by: pmarisole | last post by:
I am using the following code to split/join values in a multi-select field. It is combining all the values in All the records into one long string in each record in recordset. Example: I have a...
1
by: EoRaptor013 | last post by:
Not sure where to ask this question, but... I'm using a TreeView component to enable browsing file folders in a specific directory (for test purposes /Program Files/). Some users use an ampersand...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
3
by: albert.neu | last post by:
Hello! What is the difference between "library parts" of C99 and "language parts" of C99. see...
0
NeoPa
by: NeoPa | last post by:
Intention : To prepare a WHERE clause for multiple field selection, but to ignore any fields where the selection criteria are not set. ONLY WORKS WITH TEXT FIELD SELECTIONS. Scenario : You have...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.