Unable to use multiple date formats for moment due to format naming function

Unable to use multiple date formats for moment due to format naming function

JammySlayerJammySlayer Posts: 43Questions: 12Answers: 2

Link to test case:
https://live.datatables.net/culafofa/1/

Error messages shown:
Uncaught TypeError: t.replace is not a function
De https://localhost/lib/datatables/2.0.5/datatables.min.js:41
datetime https://localhost/lib/datatables/2.0.5/datatables.min.js:41
<anonymous> https://localhost/js/utilities/datatables/2.0/defaults.js?v=:92

Uncaught TypeError: name.replace is not a function
__mldFnName https://localhost/lib/datatables/2.0.5/datatables.js:97264
datetime https://localhost/lib/datatables/2.0.5/datatables.js:97442
<anonymous> https://localhost/js/utilities/datatables/2.0/defaults.js?v=:92

Description of problem:
Can't use multiple formats with moment, due to it attempting to set a name up for the format that expects a string, where moment can accept an array

Potential fix
function __mldFnName(name) {
if (Array.isArray(name)) {
return name[0].replace(/[\W]/g, '')
}
return name.replace(/[\W]/g, '
')
}

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    Answer ✓

    The test case doesn't show those errors.

    You have the wrong format defined for the End Date: 05/7/2012. You have DD/MM/YYYY but it should be DD/M/YYYY with one M instead of two. See the Moment format docs.

    The DataTable.datetime() specifies it takes a string for the format not an array. Although the array seems to work with the updated format:

    DataTable.datetime(['DD MMM YYYY','DD/M/YYYY']);
    

    Updated example:
    https://live.datatables.net/culafofa/2/edit

    It might be better to follow the docs and supply a string by using DataTable.datetime() multiple times, for example:

    DataTable.datetime('DD MMM YYYY');
    DataTable.datetime('DD/M/YYYY');
    

    Example:
    https://live.datatables.net/kavecejo/1/edit

    Kevin

  • JammySlayerJammySlayer Posts: 43Questions: 12Answers: 2

    That's odd could of sworn it wasn't working in the wasn't working :D Guess I need to pay more attention to my own example, must have been fixed since 2.0.5 which is the version I was getting the issue on

  • JammySlayerJammySlayer Posts: 43Questions: 12Answers: 2

    Ooh I see why my example wasn't working, I had an extra M in the format, so it didn't accept one letter months.

    Figured the lack of an error was something else :)

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I changed how the naming is done for 2.1. There was potential for overlap based on the non-formatting characters as you say.

    Allan

Sign In or Register to comment.