简体   繁体   English

使用moment.js在当前语言环境中格式化日期直到月

[英]Format date in current locale until month using momentjs

Moment.js has a handy formats ( l , L , ll , LL , lll , LLL , llll and LLLL ) for formatting a date in the current locale. Moment.js具有方便的格式( lLllLLlllLLLllllLLLL ),用于格式化当前语言环境中的日期。

The problem is that this formats date including the day. 问题是这种格式的日期包括日期。 For example moment('1986-02-05').format('LL') will be output as February 5, 1986 . 例如moment('1986-02-05').format('LL')将输出为February 5, 1986

The reason why this doesn't work for me is that I only want to display the day, only the month. 之所以对我不起作用,是因为我只想显示日期,只显示月份。 In English that would be February 1896 . 用英语讲是February 1896

Is there a way of formatting only month and year according to a locale? 有没有一种方法可以根据语言环境仅格式化月份和年份? If not with momentjs, with any other library. 如果不使用momentjs,则使用任何其他库。 I'd also take a link to a list of the strings that I could pass to .format for common locales. 我还会链接到可以传递给.format的通用语言环境的字符串列表。

Please refer bellow code for required format, it will work : 请参考以下代码以获取所需格式,它将起作用:

moment.locale('en');
var date = moment(new Date());
console.log(date.format('MMMM YYYY'));

See momentjs format() docs for a list of tokens that you can use to show date and time. 有关可用于显示日期和时间的标记列表,请参见momentjs format()文档。

In your case, the relevant tokens are MMM for month name and YYYY for the year. 在您的情况下,相关令牌是月名的MMM和年份的YYYY Don't forget to load each locale you need to use, as described here . 不要忘了加载每个需要使用的语言环境,如所描述这里

You can use locale() function to change the locale of the given moment instance and moment.locale if you need you can change locale globally . 您可以使用locale()函数更改给定矩实例和moment.locale的区域设置,如果需要,可以全局更改区域设置


EDIT: 编辑:

There is no built-in method to get the localized input you are looking for, but you can use moment localeData and longDateFormat to get localized format for LL ( Month name, day of month, year ) and then manipulate to strip the day of month part. 没有内置方法来获取您要查找的本地化输入,但是您可以使用moment localeDatalongDateFormat来获取LL本地化格式( 月名称,月日,年 ),然后处理以longDateFormat 月中的某天部分。 The following snippet work for many locale, but it doesn't show the expected output for each locale (some fails: es , eu , lt , ...). 以下代码段适用于多种语言环境,但未显示每种语言环境的预期输出(某些失败: eseult ,...)。

My suggestion is to define a subset of supported locales and manually define format for edge cases. 我的建议是定义支持的语言环境的子集,并为边缘情况手动定义格式。

 function getMonthYear(value){ moment.locale(value); // Get locale data var localeData = moment.localeData(); var format = localeData.longDateFormat('LL'); // Manage custom cases if( value === 'br'){ format = 'MMMM YYYY'; } if( value === 'en-ca'){ format = 'MMMM, YYYY'; } // if( value === ...) possible other cases // Check locale format and strip day from the first part if( format.match(/^D/g) ){ format = format.replace(/^D\\./, '').replace(/^D/, ''); } format = format.trim(); return moment('1986-02-05').format(format); } ['af' , 'ar-dz', 'ar-kw', 'ar-ly', 'ar-ma', 'ar-sa', 'ar-tn', 'ar', 'az', 'be', 'bg', 'bn', 'bo', 'br', 'bs', 'ca', 'cs', 'cv', 'cy', 'da', 'de-at', 'de-ch', 'de', 'dv', 'el', 'en-au', 'en-ca', 'en-gb', 'en-ie', 'en-nz', 'eo', 'es-do', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr-ca', 'fr-ch', 'fr', 'fy', 'gd', 'gl', 'gom-latn', 'he', 'hi', 'hr', 'hu', 'hy-am', 'id', 'is', 'it', 'ja', 'jv', 'ka', 'kk', 'km', 'kn', 'ko', 'ky', 'lb', 'lo', 'lt', 'lv', 'me', 'mi', 'mk', 'ml', 'mr', 'ms-my', 'ms', 'my', 'nb', 'ne', 'nl-be', 'nl', 'nn', 'pa-in', 'pl', 'pt-br', 'pt', 'ro', 'ru', 'sd', 'se', 'si', 'sk', 'sl', 'sq', 'sr-cyrl', 'sr', 'ss', 'sv', 'sw', 'ta', 'te', 'tet', 'th', 'tl-ph', 'tlh', 'tr', 'tzl', 'tzm-latn', 'tzm', 'uk', 'ur', 'uz-latn', 'uz', 'vi', 'x-pseudo', 'yo', 'zh-cn', 'zh-hk', 'zh-tw'].forEach(localeName => { console.log( localeName + ':', getMonthYear(localeName)); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script> 

You can find similar question here and here 您可以在这里这里找到类似的问题


The following script creates and displays a Map with the number of occurences for each LL string. 以下脚本创建并显示一个Map,其中包含每个LL字符串的出现次数。 As you can see there are 68 occurences of D MMMM YYYY (targeted by the regex of the previous snippet) and few other formats covered by the previous getMonthYear . 如您所见, D MMMM YYYY发生了68次(由上一片段的正则表达式作为目标),而先前的getMonthYear涵盖了其他几种格式。 This may help you define a list of unsupported case and define a custom workaround 这可以帮助您定义不支持的案例列表并定义自定义解决方法

 let formatMap = new Map(); ['af' , 'ar-dz', 'ar-kw', 'ar-ly', 'ar-ma', 'ar-sa', 'ar-tn', 'ar', 'az', 'be', 'bg', 'bn', 'bo', 'br', 'bs', 'ca', 'cs', 'cv', 'cy', 'da', 'de-at', 'de-ch', 'de', 'dv', 'el', 'en-au', 'en-ca', 'en-gb', 'en-ie', 'en-nz', 'eo', 'es-do', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr-ca', 'fr-ch', 'fr', 'fy', 'gd', 'gl', 'gom-latn', 'he', 'hi', 'hr', 'hu', 'hy-am', 'id', 'is', 'it', 'ja', 'jv', 'ka', 'kk', 'km', 'kn', 'ko', 'ky', 'lb', 'lo', 'lt', 'lv', 'me', 'mi', 'mk', 'ml', 'mr', 'ms-my', 'ms', 'my', 'nb', 'ne', 'nl-be', 'nl', 'nn', 'pa-in', 'pl', 'pt-br', 'pt', 'ro', 'ru', 'sd', 'se', 'si', 'sk', 'sl', 'sq', 'sr-cyrl', 'sr', 'ss', 'sv', 'sw', 'ta', 'te', 'tet', 'th', 'tl-ph', 'tlh', 'tr', 'tzl', 'tzm-latn', 'tzm', 'uk', 'ur', 'uz-latn', 'uz', 'vi', 'x-pseudo', 'yo', 'zh-cn', 'zh-hk', 'zh-tw'].forEach(localeName => { var format = moment.localeData(localeName).longDateFormat('LL'); if( formatMap.has(format) ){ let val = formatMap.get(format); formatMap.set(format, val+1); } else { formatMap.set(format, 1); } }); console.log(formatMap); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM