Code Monkey home page Code Monkey logo

Comments (6)

Holt59 avatar Holt59 commented on August 19, 2024

How do you create the datable? Do you use an existing table or JSON array (or something else)?

If the data inside the datatable are of Date type (not string), then setting using the sort option should be sufficient.

from datatable.

loscar20 avatar loscar20 commented on August 19, 2024

I used an existing table, created dynamically with php.
I have tried with format 'Y/m/d', works and it orders. But I need show in format 'd/m/Y'.
Thanks.

from datatable.

Holt59 avatar Holt59 commented on August 19, 2024

You can try to use the dataTypes options:

$('#my-table').datatable({
    dataTypes: [true, true, 'date', true, ..., true]
});

You need to fill dataTypes with true for all other columns. Using 'date', the plugin will try to convert your string to:

new Date(/* initial string */);

If the default converter does not work, you can use a custom one:

function customDate(s) {
    // Here you can do whatever you want to convert your string to a date
    var dmy = s.split('/');
    return new Date(dmy[2], dmy[1], dmy[0]);
}

$('#my-table').datatable({
    dataTypes: [true, true, customDate, true, ..., true]
});

from datatable.

loscar20 avatar loscar20 commented on August 19, 2024

Works perfectly, but now it shows me the date in long format
image
And if I try to change it with the .toLocaleDateString ("es-ES") function

function customDate(s) {
     // Here you can do whatever you want to convert your string to a date
     var dmy = s.split('/');
     var options = { year: "numeric", month: "2-digit", day: "2-digit", hour12:"false"};
     return new Date(dmy[2], dmy[1], dmy[0]).toLocaleString('es-ES', options);
}

Not reorder it:
image

With the dataTypes: [true, true, 'date'...... Exactly the same happens, show the format long.

from datatable.

Holt59 avatar Holt59 commented on August 19, 2024

You want to internally store date as Date and then process the output, so you need to use the lineFormat option:

function customDate(s) {
    // Here you can do whatever you want to convert your string to a date
    var dmy = s.split('/');
    return new Date(dmy[2], dmy[1], dmy[0]);
}

$('#my-table').datatable({
    dataTypes: [true, true, customDate, true, ..., true],
    lineFormat: function(id, data) {
        var res = $('tr');
        for (var key in data) {
            if (data.hasOwnProperty(key)) {
                var value = data[key];
                if (value instanceof Date) {
                    value = toLocaleString('es-ES');
                }
                res.append('<td>' + value + '</td>');
            }
        }
    }
});

from datatable.

loscar20 avatar loscar20 commented on August 19, 2024

ok, thanks!!!! Now works perfectly.

Thanks so much!!, regards.

from datatable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.