From the line like "12/21/2017" how to get December 21 That is, from the number get the name of the month in the genitive case Without using libraries
1 answer
let inpEl = document.getElementById('test'), wYearEl = document.getElementById('year'); inpEl.addEventListener('keypress', function (e) { if (e.key === 'Enter') { this.value = getFullDateStr(this.value, wYearEl.checked) || ''; } }); inpEl.value = (new Date()).toLocaleDateString('ru-RU'); function getFullDateStr(dateStr, inclYear = true) { const MONTHS = ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря']; let dc = dateStr.match(/(\d{1,2}).(\d{1,2}).(\d{4})/); if (dc) { dc.splice(0, 1); dc[0] = +dc[0]; dc[1] = MONTHS[+dc[1] - 1]; return inclYear ? dc.join(' ') + ' г.' : `${dc[0]} ${dc[1]}`; } } Введите дату в формате [<b>d</b>]<b>d</b>_[<b>m</b>]<b>m</b>_<b>yyyy</b> (где "_" - любой символ),<br>а затем нажмите Enter:<br> <input id="test" type="text"> <label><input id="year" type="checkbox" checked="">результат с указанием года</label> Add. Information: it is not necessary to take an array into the global area (jsPerf test) .
- and if 01 is a date, then it should be December 1st, for example - werty
- @werty, exactly, I forgot about it. Now everything is ok :) Well, the input was added, for on-the-spot checking ... (if the input does not match the format, the input value is cleared). - yar85
|