14

I am trying to recover the date format according to the location. For example, if I use moment.locale('fr') retrieve "DD/MM/YYYY" or retrieve "YYYY/MM/DD" with moment.locale('en'). As you can see, I use MomentJS for my date management.

Is there a MomentJS function to recover the format?

Nemtecl
  • 355
  • 1
  • 3
  • 14
  • If I have understood your issue, then [this](https://stackoverflow.com/a/15994039/5254433) can resolve it. – abhishek gupta Apr 16 '18 at 08:04
  • see also [Get the given date format (the string specifying the format) in javascript or momentjs](https://stackoverflow.com/a/37735700/1176601) – Aprillion Apr 16 '18 at 08:04

4 Answers4

31
moment.localeData().longDateFormat('L') // "MM/DD/YYYY"

and

moment.localeData('fr').longDateFormat('L') // "DD/MM/YYYY"

seems better

6

I had the same issue, I get a more sophisticated way to solve this problem, just with:

moment().format('L')

Instead of this verbose solution with eslint warning:

moment().creationData().locale._longDateFormat.L
osmarpetry
  • 73
  • 1
  • 5
  • 1
    I am not getting it with first solution, only second works. (First gives me, eg: "10.09.2019", second gives me "DD.MM.YYYY"). – Tinmar Sep 10 '19 at 09:06
3

What I was looking for was:

moment().creationData().locale._longDateFormat.L

I found the answer thanks to Aprillion. Thanks!

osmarpetry
  • 73
  • 1
  • 5
Nemtecl
  • 355
  • 1
  • 3
  • 14
  • 7
    You can access it in a way that you don't read private variables: moment().creationData().locale.longDateFormat('L'); – Rui Marques Apr 27 '18 at 12:33
1
moment().creationData().locale.longDateFormat('L');   

// MM/DD/YYYY

working code from Rui Marques