Sorting function to sort upcoming birthdays

Sorting function to sort upcoming birthdays

StanislavStanislav Posts: 1Questions: 0Answers: 0
edited August 2011 in General
Hello, I'm trying to write a sort function to sort a column based on which birthdays are upcoming first.

This is my code so far: [code]
$(document).ready(function() {
function calculate_date(date) {
var date = date.replace(" ", "");
var now = new Date();
var nowMonth = now.getMonth() + 1;
var nowNumber = (nowMonth) * 100 + now.getDate();

if (date.indexOf('.') > 0) {
/*date a, format dd.mn.(yyyy) ; (year is optional)*/
var eu_date = date.split('.');
} else {
/*date a, format dd/mn/(yyyy) ; (year is optional)*/
var eu_date = date.split('/');
}


/*month*/
var month = eu_date[1];
if (month.length == 1) {
month = 0+month;
}

/*day*/
var day = eu_date[0];
if (day.length == 1) {
day = 0+day;
}

var currNumber = month * 100 + day;


return currNumber - nowNumber ;
}

jQuery.fn.dataTableExt.oSort['eu_date-asc'] = function(a, b) {
x = calculate_date(a);
y = calculate_date(b);

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

jQuery.fn.dataTableExt.oSort['eu_date-desc'] = function(a, b) {
x = calculate_date(a);
y = calculate_date(b);

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

[/code]
It's a modified version of one of the example sorts. My dates are in the dd/mm/yyyy format.

At the moment it just sorts the dates by dd/mm, without consideration of today's date, I can see my current logic is flawed, but I'm just out of ideas on how to tackle the problem. Any suggestions would be much appreciated.

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited August 2011
    this is off the top of my head, but should give you some ideas. some minor parsing, then using javascript functions to create dates, using the current year (ignore year fields). if date is less than today, then change the year to next year.

    returns null for any errors. you can add more robust error reporting/handling.

    [code]
    // parse date in a variety of formats and return as
    function calculate_date(datestring) {

    //detect "/" delimiter
    var date_array = datestring.split('/');

    // detect "." delimiter
    if (date_array.length == 1)
    date_array = date.string.split('.');

    // maybe you have some more formats to test for.. add them here




    // continue.. .we should have 2 or 3 fields (d/m or d/m/y)
    if (date_array.length < 2)
    return; // some kind of error

    if (date_array.length > 3)
    return; // some kind of error

    for (i in date_array)
    if (isNaN(date_array[i]))
    return; // non-numeric values passed in. error

    var today = new Date(); // get current date
    var day = date_array[0];
    var month = date_array[1] - 1;
    var year = today.getFullYear();

    try {
    var parsed_date = new Date(year, month, day);
    if (parsed_date < today) parsed_date .setFullYear(parsed_date .getFullYear()+1);
    return parsed_date;
    }
    catch (e) {
    return; // some sort of error
    }


    }
    [/code]

    then you can use regular date comparison functions to sort.
This discussion has been closed.