JRS: In article <4062dca4.0401220837.233d5e38@posting.google.com >, seen
in news:comp.lang.javascript, Colin Steadman <google@colinsteadman.com>
posted at Thu, 22 Jan 2004 08:37:02 :-[color=blue]
>I'm a stupid ASP programmer and I dont do Javascript (except for very
>simple tasks anyway),[/color]
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.
[color=blue]
> 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:[/color]
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 +.
[color=blue]
>I would therefore appreciate any help anyone could give
>me to convert this script to work with UK date format (dd/mm/yyyy).[/color]
(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.
[color=blue]
> if (!str || str.length<1) return false;
> if (!re.test(strObject.value)) return false;
> else return true;[/color]
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.