Sorting date in format dd/mm/yyyy

Hi guys,

someone have any clue for sorting date in European format like dd/mm/yyyy ?

Now it seems to work only with the format mm/dd/yyyy?

Thanks!

Destro
asked Apr 3, 2011 by anonymous

12 Answers

0 votes
Date.parse does not accept that format out of the box. It would have to be translated into something JavaScript can understand. Someone posted some code in the comments "zoggy on March 12, 2009 at 10:40 pm" that might help you figure it out, cannot vouch for it as I have not studied it.
answered Apr 3, 2011 by anonymous
0 votes
I'm sorry, not works...

         

Any idea? My job is at stake!  :D

---------------------------------------------------

i solved the problem about sorting date in 'dd/mm/yyyy' format. here is the code:

    function cp(f,c){

                var g,h,tmp,f2,c2; f=g=f.v.toLowerCase(), c=h=c.v.toLowerCase(), reg = /[0-9]{1,2}(\/|\.)[0-9]{1,2}(\/|\.)[0-9]{4}/i;

                if (!(f.match(reg) && c.match(reg))) {

                        var i=parseFloat(f.replace(/(\$|\,)/g,'')), n=parseFloat(c.replace(/(\$|\,)/g,''));

                        if(!isNaN(i)&&!isNaN(n)){g=i,h=n}

                } else {

                        // the magic happens here, swap the datetime strings dd with mm and vice-versa

                        f2 = f.split('/');

                        c2 = c.split('/');

                        tmp = f2[0];

                        f2[0] = f2[1];

                        f2[1] = tmp;

                        tmp = c2[0];

                        c2[0] = c2[1];

                        c2[1] = tmp;

                        f2 = f2.join('/');

                        c2 = c2.join('/');

                }

                i=Date.parse(f2); n=Date.parse(c2);

                if(!isNaN(i)&&!isNaN(n)){g=i; h=n}

                return g>h?1:(g<h?-1:0)

        };

it worked in ie8 for me, i hope it will work for you too :D
answered Apr 3, 2011 by anonymous
0 votes
i solved the problem about sorting date in 'dd/mm/yyyy' format. here is the code:

    function cp(f,c){

                var g,h,tmp,f2,c2; f=g=f.v.toLowerCase(), c=h=c.v.toLowerCase(), reg = /[0-9]{1,2}(\/|\.)[0-9]{1,2}(\/|\.)[0-9]{4}/i;

                if (!(f.match(reg) && c.match(reg))) {

                        var i=parseFloat(f.replace(/(\$|\,)/g,'')), n=parseFloat(c.replace(/(\$|\,)/g,''));

                        if(!isNaN(i)&&!isNaN(n)){g=i,h=n}

                } else {

                        // the magic happens here, swap the datetime strings dd with mm and vice-versa

                        f2 = f.split('/');

                        c2 = c.split('/');

                        tmp = f2[0];

                        f2[0] = f2[1];

                        f2[1] = tmp;

                        tmp = c2[0];

                        c2[0] = c2[1];

                        c2[1] = tmp;

                        f2 = f2.join('/');

                        c2 = c2.join('/');

                }

                i=Date.parse(f2); n=Date.parse(c2);

                if(!isNaN(i)&&!isNaN(n)){g=i; h=n}

                return g>h?1:(g<h?-1:0)

        };

it worked in ie8 for me, i hope it will work for you too :D

---------------------------------------------------

Question is... What about ISO format?

I've altered this piece of code to this other:

    function cp(f,c){

        var g,h,tmp,f2,c2; f=g=f.v.toLowerCase(), c=h=c.v.toLowerCase(), reg = /<strong>[0-9]{1,4}\W[0-9]{1,2}\W[0-9]{1,4}</strong>/i;

        if (!(f.match(reg) && c.match(reg))) {

            if (f.match(/R\$|€/)){f.replace(/\./g,'');f.replace(/,/,'.');}

            var i=parseFloat(f.replace(/\s?(R\$|\$|\,|€)\s?/g,'')), n=parseFloat(c.replace(/\s?(R\$|\$|\,|€)\s?/g,''));

            if(!isNaN(i)&&!isNaN(n)){g=i,h=n}

        } else {

            // the magic happens here, swap the datetime strings dd with mm and vice-versa

            f2 = f.split(<strong>/\W/</strong>);

            c2 = c.split(<strong>/\W/</strong>);

[i]            if (f2[0].length==4){

                tmp = f2[0];

                f2[0]=f2[2];

                f2[2]=tmp;

            }

[/i]            tmp = f2[0];

            f2[0] = f2[1];

            f2[1] = tmp;

[i]            if (c2[0].length==4){

                tmp = c2[0];

                c2[0]=c2[2];

                c2[2]=tmp;

            }

[/i]            tmp = c2[0];

            c2[0] = c2[1];

            c2[1] = tmp;

            f2 = f2.join('/');

            c2 = c2.join('/');

        }

        i=Date.parse(f2); n=Date.parse(c2);

        if(!isNaN(i)&&!isNaN(n)){g=i; h=n}

        return g>h?1:(g<h?-1:0)

    };

... so it parses any usual (and unsual) date separator (1-12-2005, 12.12.2010...) and to accept DD/MM/YYYY or YYYY/MM/DD formats.

Hope it helps.

Nica Mlg.
answered Apr 3, 2011 by anonymous
Thanks a lot, works!

Gracias Funciono!

Att: Jefferson Lipsky
0 votes
var sortcell_calc_value = '31/12/2010'; //31.12.2010, 31-12-2010

sortcell_calc_value = sortcell_calc_value.replace(/\b(\d)\b/g, "0$1"); //if 31.1.2010

sortcell_calc_value = sortcell_calc_value.split(/\D+/).reverse().join("");


=> number 20101231
answered Apr 3, 2011 by anonymous
0 votes
I really need help. I've tried all the examples above and in the comments of the script but I can't make it sort right.

My dateformat is <strong>d.m.y H:i:s</strong>

I've tried, as mentioned before, all the examples of fixes to this problem with editing myself, but I'm no good in javascript.

My dates are being sorted like this:

25.02.11 13:10:42

20.02.11 19:27:46

18.02.11 23:07:55

07.03.11 23:21:04

As you can se, this is DESC, so 07.03.11 should be on top.

So it's in the sorting by month the problem is.

So please help me.

And sorry about my english.

Mvh

Yankes

EDIT:

Solved this problem by adding <p style="display:none">$obj->date</p> ($obj->date is the date saved in time() format (UNIX timestamp)) before i print the actual date in my own format.

But it would still be great if someone had a solution on this problem, both to help me and others.
answered Apr 3, 2011 by anonymous
0 votes
I'm sorry, not works...

 :( :( :( :( :( :( :( :( :(         

Any idea? My job is at stake!  :D
answered Apr 3, 2011 by anonymous
0 votes
This works great. Thanks!  lol

---------------------------------------------------

No, doesn't works... :(

With IE8 raise an error "'undefined' is null or not an object" and the script doesn't work at all.

Anybody help me? Corey Ballou?  
answered Apr 3, 2011 by anonymous
0 votes
Hi Michael,

in the previous version of Tiny Table (v2.5) I solved the problem replacig this code


        var g,h; f=g=f.v.toLowerCase(); c=h=c.v.toLowerCase();

    var i=parseFloat(f.replace(/(\$|\,)/g,'')), n=parseFloat(c.replace(/(\$|\,)/g,''));

    if(!isNaN(i)&&!isNaN(n)){g=i,h=n}

    i=Date.parse(f); n=Date.parse(c);

    if(!isNaN(i)&&!isNaN(n)){g=i; h=n}

    return g>h?1:(g<h?-1:0)



with this code that someone posted


    var dX,mX,aX,fX,dY,mY,aY,fY,pos,pos2;    

    var g,h; f=g=f.v.toLowerCase(), c=h=c.v.toLowerCase();

    var i=parseFloat(f.replace(/(\€|\,)/g,'')), n=parseFloat(c.replace(/(\€|\,)/g,''));    

    if(!isNaN(i)&&!isNaN(n)){g=i,h=n}

    pos = f.indexOf("/"); pos2 = f.lastIndexOf("/");

    dX=f.substring(0,pos); mX=f.substring(pos+1,pos2); aX=f.substring(pos2+1,(pos2)+5);

    fX=mX+"/"+dX+"/"+aX;

    dY=c.substring(0,pos); mY=c.substring(pos+1,pos2); aY=c.substring(pos2+1,(pos2)+5);

    fY=mY+"/"+dY+"/"+aY;

    i=Date.parse(fX); n=Date.parse(fY);

    if(!isNaN(i)&&!isNaN(n)){g=i; h=n}

    return g>h?1:(g<h?-1:0)    



   

Now... using this code with the TinyTable V3 it work only with Firefox.

With IE8 raise an error "'undefined' is null or not an object" and the script doesen't work at all.

With v2.5 the code make possible to use your wonderfull script to europen people tha use Euro and different date format.

Do you have an idea to solve this issue?

Thank you

Destro
answered Apr 3, 2011 by anonymous
0 votes
Hi All,

Did anyone manage to resolve this issue in IE?

Would be a great help if someone has.

Cheers

Pete
answered Apr 3, 2011 by anonymous
0 votes
I came up with a modification of my previous bugfix for you. It's untested but should work in theory. Please note that it will only work for dd/mm/yyyy and not mm/dd/yyyy:

        

        function cp(f,c){

                var g,h,tmp; f=g=f.v.toLowerCase(), c=h=c.v.toLowerCase(), reg = /[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/i;

                if (!(f.match(reg) && c.match(reg))) {

                        var i=parseFloat(f.replace(/(\$|\,)/g,'')), n=parseFloat(c.replace(/(\$|\,)/g,''));

                        if(!isNaN(i)&&!isNaN(n)){g=i,h=n}

                } else {

                        // the magic happens here, swap the datetime strings dd with mm and vice-versa

                        f = f.split('/');

                        c = c.split('/');

                        tmp = f[0];

                        f[0] = f[1];

                        f[1] = tmp;

                        tmp = c[0];

                        c[0] = c[1];

                        c[1] = tmp;

                        f = f.join('/');

                        c = c.join('/');

                }

                i=Date.parse(f); n=Date.parse(c);

                if(!isNaN(i)&&!isNaN(n)){g=i; h=n}

                return g>h?1:(g<h?-1:0)

        };



You can unprettify it to your liking to make it all compact and unreadable, but this gets the point across.
answered Apr 3, 2011 by anonymous