Jump to content

[SOLVED] Javascript date help.


pepsi_max2k

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.