Get locale format with momentjs
Asked Answered
C

4

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?

Cornflower answered 16/4, 2018 at 7:51 Comment(2)
If I have understood your issue, then this can resolve it.Janeanjaneczka
see also Get the given date format (the string specifying the format) in javascript or momentjsYoungran
C
3

What I was looking for was:

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

I found the answer thanks to Aprillion. Thanks!

Cornflower answered 16/4, 2018 at 8:18 Comment(1)
You can access it in a way that you don't read private variables: moment().creationData().locale.longDateFormat('L');Milone
P
31
moment.localeData().longDateFormat('L') // "MM/DD/YYYY"

and

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

seems better

Portauprince answered 8/11, 2018 at 15:39 Comment(1)
in new moment js slash is replaced with point "MM/DD/YYYY" => "MM.DD.YYYY"Yablon
B
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
Bilbao answered 23/8, 2018 at 19:27 Comment(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").Governess
C
3

What I was looking for was:

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

I found the answer thanks to Aprillion. Thanks!

Cornflower answered 16/4, 2018 at 8:18 Comment(1)
You can access it in a way that you don't read private variables: moment().creationData().locale.longDateFormat('L');Milone
D
1
moment().creationData().locale.longDateFormat('L');   

// MM/DD/YYYY

working code from Rui Marques

Dahlberg answered 3/10, 2022 at 22:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.