Nemtecl
Nemtecl

Reputation: 355

Get locale format with momentjs

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?

Upvotes: 14

Views: 22313

Answers (4)

Mohammed Ridha
Mohammed Ridha

Reputation: 161

moment().creationData().locale.longDateFormat('L');   

// MM/DD/YYYY

working code from Rui Marques

Upvotes: 1

Cyrille Bourgois
Cyrille Bourgois

Reputation: 366

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

and

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

seems better

Upvotes: 31

Nemtecl
Nemtecl

Reputation: 355

What I was looking for was:

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

I found the answer thanks to Aprillion. Thanks!

Upvotes: 3

osmarpetry
osmarpetry

Reputation: 73

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

Upvotes: 6

Related Questions