moment get date formatted on locale

前端 未结 1 1724
盖世英雄少女心
盖世英雄少女心 2021-01-13 16:35

From the moment docs setting format as l returns in format \"M/D/YYYY\". Is there a way to get the date in just D/M or M/D format dep

1条回答
  •  盖世英雄少女心
    2021-01-13 17:05

    One way to do what you need is getting localized longDateFormat and then remove the year part with a regular expression.

    Here a working example that uses localeData and then longDateFormat. I'm not sure that it will work for each locale, but it gives the right result for fr and en-us (and probably many others).

    var locale = window.navigator.userLanguage || window.navigator.language;
    moment.locale(locale);
    
    // Get locale data
    var localeData = moment.localeData();
    var format = localeData.longDateFormat('L')
    // Remove year part
    format = format.replace(/.YYYY/, ''); 
    
    var momentTime = moment();
    console.log(momentTime.format(format));

    0 讨论(0)
提交回复
热议问题