-2

I have a datetime (mssql) in my database which looks basically like this:

2018-03-27 17:50:38.703

When I try to display it in my jQuery datatable it looks like this:

/Date(1522165838703)/   

The jQuery datatable requires this date format which is valid for sorting:

dd-MM-yy 

I have tried to convert it like this:

new Date(data).toString('dd-MM-yy')

But I'm getting value as:

Invalid Date

What am I doing wrong here? How can I convert it to a proper jQuery datatable format dd-MM-yy ?

Can someone help me out?

User987
  • 3,663
  • 15
  • 54
  • 115
  • If that is not the issue then please post an [mcve] that illustrates exactly where the problem lies. It is your responsibility to remove the code from the stack (db store all the way to presentation layer) and illustrate where the problem lies. As it is currently written that is not possible as `new Date("bad string")` would throw that error and formatting of a javascript datetime can be better answered in that duplicate. – Igor Mar 27 '18 at 16:56
  • Another contender depending on what defines a `jquery datatabale` is [format json date to mm/dd/yy format before displaying in a jquery datatable](https://stackoverflow.com/questions/28733613/). – Igor Mar 27 '18 at 16:57

1 Answers1

1

You need to write your own formatter function, try below & adjust as per your format

var a = new Date("2018-03-27 17:50:38.703");
function getDateFormatted(date){
    return date.getDate() + "-" + date.getMonth() + "-" + date.getFullYear();
}
getDateFormatted(a)

var a  = new Date("2018-03-27 17:50:38.703");
    function getFormattedDate(date){
    return date.getDate()+"-"+date.getMonth()+"-"+date.getFullYear();
    }
    console.log(getFormattedDate(a))
Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
vinayakj
  • 5,591
  • 3
  • 28
  • 48