Bruno Santos Bruno Santos - 2 years ago 204
Javascript Question

Moment.js Invalid date message

Im exploring moment.js with Datatables, and its really good for showing the date in the format that I want but there is a problem that i have with the data that I am converting.
If the date is not valid it shows the message INVALID DATE, wihch is good but not what I really want.
Is it possible to, insted of showing "Invalid Date" message, show what was in there originally?
Like if it is not a date I want to see what it is, not that message.


Thanks for your help guys!
I have done this for the datatable "aoColumns":

{ "mData": "APE",
"render": function(mData){
if(mData != null){
if(moment(mData).format("DD/MM/YYYY")== 'Invalid date')
return mData;
return moment(mData).format("DD/MM/YYYY");
sDefaultContent: ''},

Answer Source

Depending on how you've implemented Moment.js, you could simply check the return value from moment? For example

var prettyDate = moment()...;

if(prettyDate != 'Invalid date') {
    // set your date
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download