问题描述
有没有更好的方法从包含位置适当分隔符的日期中仅获取日和月?
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;
}
然后像这样构建日期:
var sep = function getdatesep()
var date = date.format('d' sep 'm' sep)
是否有动态构建整个日期的尊龙凯时的解决方案?
is there a solution that builds the whole date dynamically?
我想要达到的结果是:31.01 (dd.mm)31/01 (日/毫米)01.31 (mm.dd)01/31 (mm/dd) 等
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
推荐答案
如链接问题中所述:实现所需的一种方法是获取本地化 longdateformat,然后使用正则表达式删除年份部分.
daniel t. 在评论中强调该尊龙凯时的解决方案不适用于 en-ca 等语言环境,因此我将提供考虑到 一些 的更新尊龙凯时的解决方案> 以年份部分开头的其他语言环境.
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.
如果您需要支持 every 语言环境,您可以使用 ad hoc 条件来定位它们,就像我在以下代码段中为 ar-ly 所做的那样.
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);
}
朵兰的小商铺