显示日期和月份,根据当地情况没有一年 [英] display day and month, without a year according to locale
问题描述
我有一个首先获取分隔符的解决方案:
function getDateSep (){
var temp = moment()。format('L');
var locale = moment()。locale;
var datesep = temp.substring(5,6);
返回datesep;
}
然后生成如下所示的日期:
var sep = function getDateSep()
var date = date.format('D'+ sep +'M'+ sep)
是否有一种解决方案可以动态构建整个日期?
我想达到的结果是:
31.01(dd.mm)
31/01(dd / mm)
01.31(mm.dd)
01/31(mm / dd)etc
问题:
Daniel T.突出显示在注释中,解决方案在像 en-CA
这样的语言环境中不起作用,所以我将提供一个更新的解决方案,它考虑到一些
可能还有一些其他语言环境不支持 /。YYYY /
和 / YYYY ./
RegExp,如果您需要支持每个区域设置,您可以使用 ar-ly
中所做的那样。
各种地区:
function changeLang(value){moment.locale(value); //获取语言环境数据var localeData = moment.localeData(); var format = localeData.longDateFormat('L'); //如果(值===ar-ly){format ='D / \\\M'; } // if(value === ...)可能的其他情况//检查区域设置格式和条年如果(format.match(/ .YYYY / g)){format = format.replace(/ .YYYY /,' ); } if(format.match(/YYYY./g)){format = format.replace(/YYYY./,''); } var res = moment()。format(format); $(#result)。html(res);}
< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js>< / script>< script src =https:/ /cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment-with-locales.min.js\"> ;</script><select onchange =changeLang(value)> < option value =en> EN< / option> < option value =en-CA> en-CA(Canada)< / option> < option value =eo> eo(世界语)< / option> < option value =de> DE< / option> < option value =it> IT< / option> < option value =hu> hu(匈牙利语)< / option> < option value =ja> ja(日文)< / option> < option value =lv> lv(拉脱维亚语)< / option> < option value =fr> FR< / option> < option value =zh-hk> zh-hk - 中文(香港)< / option> < option value =ar-ly> ar-ly - 阿拉伯语(Lybia)< / option>< / select>< div id =result>< / div>
Is there any better way for getting only day and month from a date including location appropriate separator?
I have a solution that gets separator first:
function getDateSep() {
var temp = moment().format('L');
var locale = moment().locale;
var datesep = temp.substring(5, 6);
return datesep;
}
and then builds the date like this:
var sep = function getDateSep()
var date = date.format('D' + sep + 'M'+ sep)
Is there a solution that builds the whole date dynamically?
The result I want to achieve would be like: 31.01 (dd.mm) 31/01 (dd/mm) 01.31 (mm.dd) 01/31 (mm/dd) etc
As stated in the linked question: One way to do what you need is getting localized longDateFormat and then remove the year part with a regular expression.
Daniel T. highlighted in comments that the solution will not work in locales like en-CA
, so I'm going to provide an updated solution that takes in account some other locales that starts with year part.
Probably there are some other locales the are not convered with /.YYYY/
and /YYYY./
RegExp, if you need to support every locale you can target them with ad hoc condition, as I made for ar-ly
in the following snippet.
Here a code sample the shows possible output in various locales:
function changeLang(value){
moment.locale(value);
// Get locale data
var localeData = moment.localeData();
var format = localeData.longDateFormat('L');
// Manage custom cases
if( value === "ar-ly"){
format = 'D/\u200FM';
}
// if( value === ...) possible othter cases
// Check locale format and strip year
if( format.match(/.YYYY/g) ){
format = format.replace(/.YYYY/, '');
}
if( format.match(/YYYY./g) ){
format = format.replace(/YYYY./, '');
}
var res = moment().format(format);
$("#result").html(res);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment-with-locales.min.js"></script>
<select onchange="changeLang(value)">
<option value="en">EN</option>
<option value="en-CA">en-CA (Canada)</option>
<option value="eo">eo (Esperanto)</option>
<option value="de">DE</option>
<option value="it">IT</option>
<option value="hu">hu (Hungarian)</option>
<option value="ja">ja (Japanese)</option>
<option value="lv">lv (Latvian)</option>
<option value="fr">FR</option>
<option value="zh-hk">zh-hk - Chinese (Hong Kong)</option>
<option value="ar-ly">ar-ly - Arabic (Lybia)</option>
</select>
<div id="result"></div>
这篇关于显示日期和月份,根据当地情况没有一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!