Having trouble with dateformat dd.mm.yyyy

Having trouble with dateformat dd.mm.yyyy

catohacatoha Posts: 5Questions: 0Answers: 0
edited March 2012 in Plug-ins
Hi

I'm trying to sort the data format: dd.mm.yyyy

I'm using the following code:
[code]


/* Note 'unshift' does not work in IE6. A simply array concatenation would. This is used
* to give the custom type top priority
*/
jQuery.fn.dataTableExt.aTypes.unshift(
function ( sData )
{
var sValidChars = "0123456789-,";
var Char;
var bDecimal = false;

/* Check the numeric part */
for ( i=0 ; i y) ? 1 : 0));
};

jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
var ukDatea = a.split('.');
var ukDateb = b.split('.');

var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};


$(document).ready(function() {
$('#example').dataTable();
} );

[/code]

Link to where I'm trying to do the sorting: http://oraculum.catonett.com/teamactivity/behandletur.php

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    The type detection plug-in you are using is for "numeric-comma" types, but your sorting plug-in is "uk_date" - so your UK date plug-in is never going to be selected. Try this type detection plug-in: http://datatables.net/plug-ins/type-detection#uk_date

    And that should do it :-)

    Allan
  • catohacatoha Posts: 5Questions: 0Answers: 0
    Hi Allan.

    Thank you for your reply.

    I'm learning javascript, but do still have a way to go ;)

    I've modified like this, but still not working. I'm not quite sure if there is anywhere where I should replace / with .

    [code]


    /* Note 'unshift' does not work in IE6. A simply array concatenation would. This is used
    * to give the custom type top priority
    */
    jQuery.fn.dataTableExt.aTypes.unshift(
    function ( sData )
    {
    var sValidChars = "0123456789-,";
    var Char;
    var bDecimal = false;

    /* Check the numeric part */
    for ( i=0 ; i y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['numeric-comma-desc'] = function(a,b) {
    var x = (a == "-") ? 0 : a.replace( /,/, "." );
    var y = (b == "-") ? 0 : b.replace( /,/, "." );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };

    jQuery.fn.dataTableExt.aTypes.unshift(
    function ( sData )
    {
    if (sData !== null && sData.match(/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20|21)\d\d$/))
    {
    return 'uk_date';
    }
    return null;
    }
    );

    $(document).ready(function() {
    $('#deltakere2').dataTable();
    } );

    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    You've got the numeric comma plug-ins there now for sorting and type detection, and a type detection plug-in for UK date, but no sorting plug-in for it! Do you not get a JS error when trying to sort on a UK date column now?

    Allan
  • catohacatoha Posts: 5Questions: 0Answers: 0
    Yes, I noticed that now.

    I've changed the code to the following, but still without success.

    [code]


    /* Note 'unshift' does not work in IE6. A simply array concatenation would. This is used
    * to give the custom type top priority
    */


    jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function(a,b) {
    var ukDatea = a.split('.');
    var ukDateb = b.split('.');

    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
    var ukDatea = a.split('.');
    var ukDateb = b.split('.');

    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };

    jQuery.fn.dataTableExt.aTypes.unshift(
    function ( sData )
    {
    if (sData !== null && sData.match(/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20|21)\d\d$/))
    {
    return 'uk_date';
    }
    return null;
    }
    );

    $(document).ready(function() {
    $('#deltakere2').dataTable();
    } );


    [/code]
  • catohacatoha Posts: 5Questions: 0Answers: 0
    Thanks Allan. I got it to work.

    I used the following code to sort date with format dd.mm.yyyy (ex. 24.05.2012)

    [code]


    jQuery.fn.dataTableExt.aTypes.unshift(
    function ( sData )
    {
    if (sData !== null && sData.match(/^(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20|21)\d\d$/))
    {
    return 'uk_date';
    }
    return null;
    }
    );

    jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function(a,b) {
    var ukDatea = a.split('.');
    var ukDateb = b.split('.');

    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
    var ukDatea = a.split('.');
    var ukDateb = b.split('.');

    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };



    $(document).ready(function() {
    $('#example').dataTable();
    } );


    [/code]


    I hope this will help others with same issues.
This discussion has been closed.