Datetime ordering with moment.js order column alphabetically

Datetime ordering with moment.js order column alphabetically

gt.guybrushgt.guybrush Posts: 6Questions: 2Answers: 0

Hi, i have only this last problem to make DataTable works for my asp.net website.

I start from this topic: https://datatables.net/blog/2014-12-18
and download moment.js and datetime-moment.js and add them to my page

this is the start of initialization code:

<script type="text/javascript">
    $(Document).ready(function () {
        $.fn.dataTable.moment('dd/MM/YYYY');
        $.fn.dataTable.moment('L');

        var table = $('#tblCruscottoDaAvviare').DataTable(
            {
                "scrollY": "300px",
                "scrollX": true,
                "paging": false,
                "order": [],
                dom: 'Brt',
                buttons: [
                ......

From DataTable point of view table is static (html plain code in generatd my asp.net mvc razor code)

Date are in italian format: dd/MM/YYYY /for example 25/12/2017

Now when i order column with datetime, alphabetical order is used, so 01/12/2017 comes before 02/01/2010

i have to add other things that i miss?

this is the debug link:http://debug.datatables.net/amacoc
looking ot it i see that type recognition go wrong for some column:

DataStatus is data but find string
DataFinePrevista is data but find html

StatoAvanzamento is text (now all empty string) but find moment-L
SocietaService is text but find moment-L

DataAggiornamentoSAL is the only data column render correctly as moment-L but all row are empty so how can type be recognized as date?

thanks

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421
    edited August 2017

    Looks like you are overwriting the first setting with the latter:

    $.fn.dataTable.moment('dd/MM/YYYY');
    $.fn.dataTable.moment('L');
    

    if you use L you need to specify a locale for moment

    Consider this (I set moment.locale but also define a variable momentLocale to be able to send it to data tables. I also need this in the data table definitions.)

    var momentLocale;
    
    if (lang === 'it') {
       moment.locale('it');
       momentLocale = 'it';
    } else {  //or whatever other language(s) you may have
      moment.locale('en-gb');
      momentLocale = 'en-gb';
    }
    
    $.fn.dataTable.moment( 'L', momentLocale );
    

    This works fine for me. Don't forget the moment locale files.

    <!--    Locales for moment.js-->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/locale/it.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/locale/en-gb.js"></script>
        <script src="https://cdn.datatables.net/plug-ins/1.10.15/sorting/datetime-moment.js"></script>
    

    This is how I use momentLocale in the table definitions:

    table: "#yourTable",
            fields: [{
                    label: "Start Date:",
                    name: "yourDate",
                    attr: {
                        class: dateMask
                    },
                    type: "datetime",
                    format: 'L',
                    opts: {
                        showWeekNumber: true,
                        momentLocale: momentLocale
                    }
                },
    
  • gt.guybrushgt.guybrush Posts: 6Questions: 2Answers: 0
    $.fn.dataTable.moment('dd/MM/YYYY');
    $.fn.dataTable.moment('L');
    

    was a test, i can leave only the first without using locale (i have only italian format to manage)

    column are variable and not fixed (datasource can change and table is dinamically adatpted) so i can't use 'fields: ' to specify format

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421
    edited August 2017 Answer ✓

    If that is the Italian format 25/12/2017 and you are using this $.fn.dataTable.moment('dd/MM/YYYY') then it is obviously not the same!

    This is from the italian moment locale file:

    var it = moment.defineLocale('it', {
        months : 'gennaio_febbraio_marzo_aprile_maggio_giugno_luglio_agosto_settembre_ottobre_novembre_dicembre'.split('_'),
        monthsShort : 'gen_feb_mar_apr_mag_giu_lug_ago_set_ott_nov_dic'.split('_'),
        weekdays : 'domenica_lunedì_martedì_mercoledì_giovedì_venerdì_sabato'.split('_'),
        weekdaysShort : 'dom_lun_mar_mer_gio_ven_sab'.split('_'),
        weekdaysMin : 'do_lu_ma_me_gi_ve_sa'.split('_'),
        longDateFormat : {
            LT : 'HH:mm',
            LTS : 'HH:mm:ss',
            L : 'DD/MM/YYYY',
            LL : 'D MMMM YYYY',
            LLL : 'D MMMM YYYY HH:mm',
            LLLL : 'dddd, D MMMM YYYY HH:mm'
        },
    

    from the moment.js website:

    moment.locale();         // it
    moment().format('LT');   // 10:39
    moment().format('LTS');  // 10:39:07
    moment().format('L');    // 18/08/2017
    moment().format('l');    // 18/8/2017
    moment().format('LL');   // 18 agosto 2017
    moment().format('ll');   // 18 ago 2017
    moment().format('LLL');  // 18 agosto 2017 10:39
    moment().format('lll');  // 18 ago 2017 10:39
    moment().format('LLLL'); // venerdì, 18 agosto 2017 10:39
    moment().format('llll'); // ven, 18 ago 2017 10:39
    

    use $.fn.dataTable.moment('DD/MM/YYYY'), no need to tuse the locale files then.

  • gt.guybrushgt.guybrush Posts: 6Questions: 2Answers: 0
    edited August 2017

    was so simple but great idea looking at italian locale

    in many other language 'd' is for days and not 'D'...

    thank you very much

This discussion has been closed.