angularjsdatesmart-table

Javascript dd-mm-yyyy date format


I use st-table of angular js. One of the columns, there is date information which has to be in format dd-mm-yyyy. I have found some ways to convert it like;

new Date(year,month,day).toLocaleDateString('en-US')

but, on that case, it is converted to string and when I order the column by clicking it, since it is string the first of any date comes first when it is ascending and vice versa, for this reason it must be date not string


Solution

  • Since you are using angular, the 'Angular way" would be to use Angular's date filter. The format strings are standard, and are documented there, but I've provided the format you are looking for.

    You show a lowercase 'm' in your question; please not that is for minute, not Month.

    HTML Side

    {{ yourDateObj | date:'dd-MM-yyyy' }}
    

    Controller / JS side:

    $filter('date')(yourDateObj,'dd-MM-yyyy')
    

    Smart Grid Details

    That all said, re-reading your question made me wonder. I did find a plnkr here from the smart-table site, which shows that the value should be a date, and not a string. The code they are using does use an angular filter on the HTML side. The array of data that is provided leverages a Date object. In that case, you should provide that date using new Date(year,month,day) as you've mentioned, but without the invocation of .toLocaleDateString() (or any other method).