0

I'm trying to display a properly formatted date in a Datatable column. I'm using MomentJS to detect and format the cell properly, as it comes as a yyyyMMdd (i.e. 20051201) string from the database:

columns: [
    {
        data: "fechaPago", 
        "render": function(data){
            return (moment(data).isValid()) ? moment(data).format("DD/MM/YYYY") : "-";
        }
    },
]

Which works exactly as expected:

enter image description here

Now, I'm struggling to implement a more flexible approach, and let the current locale determine the format. We have 3 available languages in our webapp, and I want the Datatable to detect the current language and change the format (from dd/MM/yyyy to MM/dd/yyyy, if it's in English).

I have tried to use 'LLLL' as the format, but it returns the whole date with letters, and I can't figure out how to modify it.

VincenzoC
  • 30,117
  • 12
  • 90
  • 112
xabi_sides
  • 335
  • 3
  • 20
  • 1
    Does [`format('L')`](http://momentjs.com/docs/#/displaying/format/) satisfy your needs? – VincenzoC May 04 '17 at 12:45
  • @VincenzoC Thanks for the help Vincenzo. It partially does, as it simplifies the formatting a lot. However, it's not changing the format when I change the language. – xabi_sides May 04 '17 at 12:50
  • Do you already have locale info in your code or you are asking how to get browser locale (in this case search on SO, e.g. see [here](http://stackoverflow.com/q/1043339/4131048))? – VincenzoC May 04 '17 at 12:55
  • @VincenzoC I guess I could import $locale into the directive to get the current language, but I can't figure how to take into account the conditional in the render (if -> locale = "pt-pt" -> then -> show me the date in "dd/MM/yyyy" format). – xabi_sides May 04 '17 at 13:07

1 Answers1

1

You can use moment locale(String) function to set locale for a moment oblect or moment.locale(String) to set moment locale globally.

To get localized day, month and year use L token in the format().

Your code will be like the following:

columns: [
  {
    data: "fechaPago",
    "render": function(data){
      var locale = // get current locale code
      return (moment(data).isValid()) ? moment(data).locale(locale).format("L") : "-";
    }
  },
]

Remember to use moment-with-locales.js (or import required locales).

VincenzoC
  • 30,117
  • 12
  • 90
  • 112
  • Hi again Vincenzo. I tried using your solution, and even though yyyyMMdd string gets formatted to MM-dd-yyyy, it does not change when I switch the global locale, not even when I reload. – xabi_sides May 04 '17 at 14:42
  • 1
    @wickedchild the problem could be the way you get locale, try to test my solution hardcoding locale (`var locale = 'en-gb';`) and rember to use [`moment-with-locales.js`](https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js) (or import required locales). – VincenzoC May 04 '17 at 14:50
  • That's it! I was using the regular moment.js, switching to the other one did the trick :) Thanks a lot for your time, Vincenzo :) – xabi_sides May 04 '17 at 14:56
  • You're welcome! I've edited my answer adding the info of my last comment. – VincenzoC May 04 '17 at 15:05