pepsi_max2k Posted July 2, 2009 Share Posted July 2, 2009 Hey all, I'm currently trying to edit a small bit of javascript to get it working with UK dates, and it's doing my head in The code is a table sorting program from http://www.javascripttoolbox.com/lib/table/source.php , the part in question is as follows, and it's set up to use US dates but I'm trying to use it with UK dates like 30-01-09 (30th Jan 09). // Date sort // --------- sort.date = function(a,b) { return sort.numeric(sort.date.convert(a),sort.date.convert(b)); }; // Convert 2-digit years to 4 sort.date.fixYear=function(yr) { yr = +yr; if (yr<50) { yr += 2000; } else if (yr<100) { yr += 1900; } return yr; }; sort.date.formats = [ // YY[YY]-MM-DD { re:/(\d{2,4})-(\d{1,2})-(\d{1,2})/ , f:function(x){ return (new Date(sort.date.fixYear(x[1]),+x[2],+x[3])).getTime(); } } // MM/DD/YY[YY] or MM-DD-YY[YY] ,{ re:/(\d{1,2})[\/-](\d{1,2})[\/-](\d{2,4})/ , f:function(x){ return (new Date(sort.date.fixYear(x[3]),+x[1],+x[2])).getTime(); } } // Any catch-all format that new Date() can handle. This is not reliable except for long formats, for example: 31 Jan 2000 01:23:45 GMT ,{ re:/(.*\d{4}.*\d+:\d+\d+.*)/, f:function(x){ var d=new Date(x[1]); if(d){return d.getTime();} } } ]; sort.date.convert = function(val) { var m,v, f = sort.date.formats; for (var i=0,L=f.length; i<L; i++) { if (m=val.match(f[i].re)) { v=f[i].f(m); if (typeof(v)!="undefined") { return v; } } } return 9999999999999; // So non-parsed dates will be last, not first }; To start with I've no idea how javascript work, so I've just been hacking away to see what happens. The best I have is: sort.date.formats = [ // YY[YY]-MM-DD { re:/(\d{1,2})-(\d{1,2})-(\d{2,4})/ , f:function(x){ return (new Date(sort.date.fixYear(+x[1],+x[2]),+x[3])).getTime(); } } which seems to sort the years and the months properly, but the days are now in reverse, eg: 09-02-09 10-02-09 11-02-09 09-01-09 10-01-09 11-01-09 09-12-09 Anyone know how to edit it to use with uk dates? I don't really wanna use US ones but it's looking like the simplest option. Link to comment https://forums.phpfreaks.com/topic/164551-solved-javascript-date-help/ Share on other sites More sharing options...
pepsi_max2k Posted July 2, 2009 Author Share Posted July 2, 2009 Figured it out eventually. Add this in to replace the current date sorting code. Works fine for DD-MM-YY. sort.date.formats = [ // DD-MM-YY[YY] { re:/(\d{1,2})-(\d{1,2})-(\d{2,4})/ , f:function(x){ return (new Date(sort.date.fixYear(x[3]),+x[2],+x[1])).getTime(); } } // Any catch-all format that new Date() can handle. This is not reliable except for long formats, for example: 31 Jan 2000 01:23:45 GMT ,{ re:/(.*\d{4}.*\d+:\d+\d+.*)/, f:function(x){ var d=new Date(x[1]); if(d){return d.getTime();} } } ]; Link to comment https://forums.phpfreaks.com/topic/164551-solved-javascript-date-help/#findComment-867999 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.