With MomentJS, if necessary I can set a locale on a specific moment instance (rather than globally) using .locale
(or .lang
in older versions). How can I parse a string using a specific locale other than the global one? Since the moment
function itself is what we use for parsing, and there doesn't seem to be a .set
variant that does full parsing?
E.g.:
// Assume the default locale is 'en' (just in case, I'll set it for the snippet)
moment.locale('en');
// I have this string that's in the French locale
var str = "30 Avril 2015";
// Parsing it fails
var m = moment(str, "DD MMMM YYYY");
snippet.log(m.format("YYYY-MM-DD")); // "Invalid Date"
<script src=".js/2.9.0/moment-with-locales.js"></script>
<!-- Script provides the `snippet` object, see -->
<script src=".js"></script>
With MomentJS, if necessary I can set a locale on a specific moment instance (rather than globally) using .locale
(or .lang
in older versions). How can I parse a string using a specific locale other than the global one? Since the moment
function itself is what we use for parsing, and there doesn't seem to be a .set
variant that does full parsing?
E.g.:
// Assume the default locale is 'en' (just in case, I'll set it for the snippet)
moment.locale('en');
// I have this string that's in the French locale
var str = "30 Avril 2015";
// Parsing it fails
var m = moment(str, "DD MMMM YYYY");
snippet.log(m.format("YYYY-MM-DD")); // "Invalid Date"
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<!-- Script provides the `snippet` object, see http://meta.stackexchange./a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Share
Improve this question
edited Apr 3, 2015 at 15:23
T.J. Crowder
asked Apr 3, 2015 at 15:18
T.J. CrowderT.J. Crowder
1.1m200 gold badges2k silver badges2k bronze badges
4
- 1 I posted it as it might be useful to someone anyway? I think I get what you want, you want some sort of "scope" where you can work on french dates for instance, without messing with the global locale. – adeneo Commented Apr 3, 2015 at 15:35
- @adeneo: Agreed. And yes, that's what I want. I'll post it separately. – T.J. Crowder Commented Apr 3, 2015 at 15:37
-
You'd think there was a way to do that, but I can't remember ever seeing something like that? The documentation is rather extensive and I haven't read all of it, but I can't spot anything that would work like that, and you've probably looked as well. Could one perhaps extend moment to have a
set()
method on an instance or something like that ? – adeneo Commented Apr 3, 2015 at 15:40 - @adeneo: FYI, I've posted the question. – T.J. Crowder Commented Apr 3, 2015 at 15:41
1 Answer
Reset to default 6As of version 2.0.0
, a locale key can be passed as the third parameter to moment()
and moment.utc()
// Assume the default locale is 'en' (just in case, I'll set it for the snippet)
moment.locale('en');
// I have this string that's in the French locale
var str = "30 Avril 2015";
// Parse it in 'fr'
var m = moment(str, "DD MMMM YYYY", "fr");
// Check result:
snippet.log(m.format("YYYY-MM-DD -- MMMM"));
// Check global locale
var locale = moment()._locale._abbr;
snippet.log('Locale : ' + locale);
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<!-- Script provides the `snippet` object, see http://meta.stackexchange./a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745260831a4619205.html
评论列表(0条)