2

How can I localize current day and month (without year) in moment.js? What I want is exactly the output of moment().format('LL') but without the year part.

Consider the following example:

moment().locale('tr').format('LL') // "1 Haziran 2017"
moment().locale('en').format('LL') // "June 1, 2017"

What I want is these:

moment().locale('tr').format('??') // "1 Haziran"
moment().locale('en').format('??') // "June 1"
smddzcy
  • 443
  • 4
  • 19

3 Answers3

6

For error prone solution for all supported locales, you need to remove year with .replace and check for unnecessary symbols left:

function getCurrDayAndMonth(locale) {
  var today = locale.format('LL');
  return today
    .replace(locale.format('YYYY'), '') // remove year
    .replace(/\s\s+/g, ' ')// remove double spaces, if any
    .trim() // remove spaces from the start and the end
    .replace(/[рг]\./, '') // remove year letter from RU/UK locales
    .replace(/de$/, '') // remove year prefix from PT
    .replace(/b\.$/, '') // remove year prefix from SE
    .trim() // remove spaces from the start and the end
    .replace(/,$/g, ''); // remove comma from the end
}

['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 + ':',
    getCurrDayAndMonth(moment().locale(localeName)));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
terales
  • 3,116
  • 23
  • 33
0

Do you just want the string representation? If so, it might be easier to just trim the last 5 chars from the end of the output, like so:

var today = moment().locale('tr').format('LL') // "1 Haziran 2017"
today = today.substring(0, today.length - 5); // "1 Haziran"

This'll work for the next 8000 years, so no real need to worry about future years breaking it.

You could even do smarter regex matching, or just remove everything with ", 20XX" from the string. Depends on your uses of it though, this is more of a hacky workaround than a direct solution.

JonLuca
  • 850
  • 6
  • 25
  • This is the obvious solution, but I want to avoid it because I don't know how exactly `format('LL')` works. The date might not end with the year, or the locale could use a different number system. By the way, yes, string representation is enough :) – smddzcy Jun 01 '17 at 12:18
  • Ah understood. You want this to work for every language? I'd recommending converting ot locale, and then selecting only D and M with the built in format. I just glanced over the moment source code and I don't believe there's a way to take built in locale format and only remove one part of it (i.e. the year). You have to apply the formatting you want (like webbm did above) – JonLuca Jun 01 '17 at 12:37
0

Here's an example of how it can work:

var d = moment().locale('tr');
console.log(d.format('D MMMM'));

JSFiddle: https://jsfiddle.net/webbm/wuwkwzou/

webbm
  • 634
  • 8
  • 18
  • `moment().locale('en').format('D MMMM')` is `1 June`. This is definitely not what I want. Please take a look at my example. – smddzcy Jun 01 '17 at 12:52