Table Sort With Dates (UK date format)

C

Colin Steadman

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/kaelisSpace/useful_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. */
 
D

DJ WIce

: 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
 
M

Michael Winter

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
 
M

Martin Honnen

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

Michael Winter

: 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
 
K

kaeli

[email protected] enlightened said:
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.

--
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
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/kaelisSpace/useful_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.
 
C

Colin Steadman

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
 
C

Colin Steadman

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
 
K

kaeli

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.

--
 
D

DJ WIce

: 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/sorttable/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
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top