By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,688 Members | 1,897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,688 IT Pros & Developers. It's quick & easy.

Table Sort With Dates (UK date format)

P: n/a
I'm a stupid ASP programmer and I dont do Javascript (except for very
simple tasks anyway), and I'm in a bit of a predicament. I've used a
javascript table sorting script from here:

http://www.ipwebdesign.net/kaelisSpa...tableSort.html

This works great except it doesn't sort my UK formatted dates
properly, and I end up with something like this:

Birth Date (dd/mm/yyyy)
=======================
01/10/1990
01/05/1981
01/02/1956
01/08/1977
02/12/1944
The author obviously foresaw this problem and helpfully left a comment
in the script describing how to solve the problem (I have copied the
note at the bottom of this post). The solution is to:

"Modify function isDate if the field has a date in another format.",

I have therefore studied the IsDate function in hopes of making this
change. But having done this I can honestly say I dont have a clue
how it works. I would therefore appreciate any help anyone could give
me to convert this script to work with UK date format (dd/mm/yyyy).

TIA,

Colin
function isDate(str)
{
/* Returns true if the field is mm/dd/yyyy because for some reason,
the browser is not giving the expected NaN for these fields */

var re = /^[01]?[0-9]\/[0-9]?[0-9]\/[12][0-9][0-9][0-9]$/;

if (!str || str.length<1) return false;
if (!re.test(strObject.value)) return false;
else return true;
}


/* parseFloat parses its argument, a string, and returns a floating
point number. If it encounters a character other than a sign (+
or -),
numeral (0-9), a decimal point, or an exponent, it returns the
value
up to that point and ignores that character and all succeeding
characters. Therefore, a regular expression is used to determine
if the field is a date in mm/dd/yyyy format, since it will not sort
the field properly (it does not return NaN and sort the field as a
string, as you might expect it to do). Modify function isDate if
the field has a date in another format. */
Jul 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

: me to convert this script to work with UK date format (dd/mm/yyyy).
:
: var re = /^[01]?[0-9]\/[0-9]?[0-9]\/[12][0-9][0-9][0-9]$/;

var re = /^[0123]?[0-9]\/[01]?[0-9]\/[12][0-9][0-9][0-9]$/;

Wouter
Jul 20 '05 #2

P: n/a
On 22 Jan 2004 08:37:02 -0800, Colin Steadman <go****@colinsteadman.com>
wrote:
function isDate(str)
{
/* Returns true if the field is mm/dd/yyyy because for some reason,
the browser is not giving the expected NaN for these fields */

var re = /^[01]?[0-9]\/[0-9]?[0-9]\/[12][0-9][0-9][0-9]$/;

if (!str || str.length<1) return false;
if (!re.test(strObject.value)) return false;
else return true;
}


Try replacing the regular expression (backing up the old one, obviously)
with:

var re = /^[0-9]?[0-9]\/[01]?[0-9]\/[12][0-9][0-9][0-9]$/

That just switches around the portion that validates the month and day
part.

I haven't tried it - you're in the better position to do that.

Good luck,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #3

P: n/a


Colin Steadman wrote:

function isDate(str)
{
/* Returns true if the field is mm/dd/yyyy because for some reason,
the browser is not giving the expected NaN for these fields */

var re = /^[01]?[0-9]\/[0-9]?[0-9]\/[12][0-9][0-9][0-9]$/;

if (!str || str.length<1) return false;
if (!re.test(strObject.value)) return false;
else return true;
}


Are you sure that function is used at all/works in the code you have
downloaded? It has a parameter named str but later calls
re.test(strObject.value)
Unless there is a global variable named strObject somewhere calling
isDate should yield an error.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #4

P: n/a
On Thu, 22 Jan 2004 18:06:28 +0100, DJ WIce <co*********@djwice.com> wrote:
: var re = /^[01]?[0-9]\/[0-9]?[0-9]\/[12][0-9][0-9][0-9]$/;

var re = /^[0123]?[0-9]\/[01]?[0-9]\/[12][0-9][0-9][0-9]$/;


I thought of correcting the possible 99/12/2004, too. However, there's not
much point; the expression will still allow 39/19/2004, which is equally
wrong, just not quite as ridiculously so. Best leave it to the Date
object...

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #5

P: n/a
In article <40********@olaf.komtel.net>, ma*******@yahoo.de enlightened
us with...

Are you sure that function is used at all/works in the code you have
downloaded? It has a parameter named str but later calls
re.test(strObject.value)
Unless there is a global variable named strObject somewhere calling
isDate should yield an error.


Good catch.
Damn copy/paste errors.

Thanks.

--
--
~kaeli~
If you don't pay your exorcist, you get repossessed.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #6

P: n/a
JRS: In article <40**************************@posting.google.com >, seen
in news:comp.lang.javascript, Colin Steadman <go****@colinsteadman.com>
posted at Thu, 22 Jan 2004 08:37:02 :-
I'm a stupid ASP programmer and I dont do Javascript (except for very
simple tasks anyway),
Which is why you should have sought the answer in the FAQ. That will
also get you to proper validation of dates, including Feb 29, and co-
efficient conversion to a Date Object.
and I'm in a bit of a predicament. I've used a
javascript table sorting script from here:

http://www.ipwebdesign.net/kaelisSpa...tableSort.html

This works great except it doesn't sort my UK formatted dates
properly, and I end up with something like this:
That is, as it should be, alphanumeric order.

The first question is how many dates do you have - a few, or a lot?

Sorting takes time in excess of o(N), so more preparatory work is
justified if N is large. In that case, you can convert the dates into
date objects and the them as sort keys. Otherwise, you just need to
convert for each comparison.

You cannot read UK dates directly into a Date Object using an arbitrary
Web browser (the default assumption here), since at least some browsers
use FFF dates, mm/dd/yyyy. It is conceivable that an Intranet might
have browsers configured for UK dates (I do not know of any); but if
yours were so you presumably would not be asking.

Either of the following converts a date to sortable form :-

var X = '29/07/2002' // becomes 2002379
X = X.split(/\D+/)
X = -( (-X[2]*20-X[1])*50 - X[0] ) // or *100 *100 for 20020729

function dc(D) { // gives YYYYMMDD
var x = /(\d+)\D+(\d+)\D+(\d+)/.test(D)
with (RegExp) return ($3*100 + +$2)*100 + +$1 }

Note the use of unary + as well as binary +.

I would therefore appreciate any help anyone could give
me to convert this script to work with UK date format (dd/mm/yyyy).
(1) Delete it.
(2) Read the FAQ and what it links to - see below.
(3) Start again.

Note : if the dates are guaranteed valid, there is no need to validate
them. The methods above do NOT require the leading zeroes.
if (!str || str.length<1) return false;
if (!re.test(strObject.value)) return false;
else return true;


or? return str && str.length>=1 && re.test(strObject.value)

However, the middle test ought to be superfluous, since, if it gives
false, the following test should give false rapidly enough.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #7

P: n/a
> Are you sure that function is used at all/works in the code you have
downloaded? It has a parameter named str but later calls
re.test(strObject.value)
Unless there is a global variable named strObject somewhere calling
isDate should yield an error.

Apoligies if this posts appears twice. I'm posting from Google and
its playing silly beggers with me:
================================================== =============================

No I'm not at all sure. I dont see that variable anywhere else in the
script either. I've blindly tried changing:

if (!re.test(strObject.value)) return false;

to this:

if (!re.test(str.value)) return false;

and this:

if (!re.test(str)) return false;

But, both changes fail to fix the problem.

Colin
Jul 20 '05 #8

P: n/a
> I thought of correcting the possible 99/12/2004, too. However, there's not
much point; the expression will still allow 39/19/2004, which is equally
wrong, just not quite as ridiculously so. Best leave it to the Date
object...

Mike

The suggested changes have not corrected the problem so I'm not overly
concerned about it allowing 39/10/2004. And as mentioned by another
poster, the function seems to be invalid anyway.

This doesn't look like there is going to be a quick fix so I'll eather
live with it or find another script. If you know of any I'm all ears!

Regards,

Colin
Jul 20 '05 #9

P: n/a
In article <40**************************@posting.google.com >,
go****@colinsteadman.com enlightened us with...

if (!re.test(str)) return false;


Use that one.
And change compareValues to

function compareValues(v1, v2) {

var f1, f2;
// If the values are numeric, convert them to floats.
/* parseFloat parses its argument, a string, and returns a floating
point number.
If it encounters a character other than a sign (+ or -), numeral (0-
9), a
decimal point, or an exponent, it returns the value up to that point
and
ignores that character and all succeeding characters. Therefore, a
regular
expression is used to determine if the field is a date in
mm/dd/yyyy format,
since it will not sort the field properly (it does not return NaN
and sort
the field as a string, as you might expect it to do).
Modify function isDate if the field has a date in another format.
It only returns NaN if the first character cannot be converted to a
number.
*/

f1 = parseFloat(v1);
f2 = parseFloat(v2);
if (!isDate(v1) && !isNaN(f1))
v1 = f1
if (!isDate(v2) &&!isNaN(f2))
v2 = f2;

// Compare the two values.
if (v1 == v2)
return 0;
if (v1 > v2)
return 1
return -1;
}

And also change the regular expression to one that suits you (Michael
Winter posted one). The regex that is in that script is US mm/dd/yyyy
format.

Note that the sort is as text, not as a date.
If you really want a true date sort, see the FAQ that was mentioned.

--
--
~kaeli~
He had a photographic memory that was never developed.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #10

P: n/a
: Birth Date (dd/mm/yyyy)
: =======================
: 01/10/1990
: 01/05/1981
: 01/02/1956
: 01/08/1977
: 02/12/1944

I think your script comes from:

http://www.mattkruse.com/javascript/...le/source.html

Look in the documentation:

t.AddLine("Firstname Surname","01/10/1990");
t.AddLineSortData("Surname, Firstname","10/01/1990");

This will solve your problem; adding a "how to sort the above line" entry
after each normal entry.

Ciao,
Wouter
Jul 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.