更新时间:2023-01-31 14:35:55
好的。这有点糟糕,但你知道它会是。
Okay. This is a little awful, but you knew it was going to be.
首先,你可以访问(例如)的实际格式字符串'L'
:
First, you can access the actual format string for (for instance) 'L'
:
var formatL = moment.localeData().longDateFormat('L');
接下来,您可以通过明智的正则表达式替换来执行一些手术:
Next, you can perform some surgery on it with judicious regex replacement:
var formatYearlessL = formatL.replace(/Y/g,'').replace(/^\W|\W$|\W\W/,'');
(也就是说:删除YYYY,加上删除后留下的孤立分隔符)
(Which is to say: Remove YYYY, plus the orphaned separator left by its removal)
然后你可以在片刻格式调用中使用你的新格式字符串:
Then you can use your new format string in a moment format call:
someDate.format(formatYearlessL);
这必然会做出一些假设:
This necessarily makes some assumptions:
快速查看 locale / * .js
,这些假设适用于每个语言环境文件我检查过,但可能有一些违反它们的区域设置。 (ETA:下面的评论指出德国短日期格式违反了第二个假设)
On a quick review of locale/*.js
, these assumptions hold true for every locale file I examined, but there may be some locales that violate them. (ETA: a comment below points out that a German short date format violates the second assumption)
作为一个额外的重要警告,这可能是脆弱的。未来版本的moment.js完全有可能改变当前数据的位置 longDateFormat
...
As an additional important caveat, this is likely to be fragile. It is entirely possible that a future version of moment.js will change the location of the data currently in longDateFormat
...