Здравствуйте, у меня возникла некая проблемка, нужно убрать "Г." при выводе даты в js.
function getLocalYearMonth(d) {
return d.toLocaleDateString(locale, {
year: '2-digit',
month: 'long'
});
}
var nDate = new Date(year, month, 1);
var title = document.getElementById('c-title');
title.innerHTML = getLocalYearMonth(nDate);
При выводе получается примерно так: "Апрель 18Г.", мне нужно чтобы выводило просто "Апрель 18".
Кроме двух вариантов костыля ничего в голову не приходит.
А заглавную букву можно потом сделать в css при помощи text-transform: capitalize.
function getLocalYearMonth(d) {
return d.toLocaleDateString("ru-RU", { month: 'long' }) + " " +
d.toLocaleDateString("ru-RU", { year: '2-digit' });
}
var nDate = new Date();
console.log(getLocalYearMonth(nDate));
function getLocalYearMonth(d) {
return d.toLocaleDateString("ru-RU", { month: 'long', year: '2-digit' })
.replace(/\s*г\./, "");
}
var nDate = new Date();
console.log(getLocalYearMonth(nDate));
Просто slice(0, -3).
Вернет строку с "отрезанными" тремя последними символами (в данном случае "_г.")
function getLocalYearMonth(d) {
return d.toLocaleDateString('ru-RU', {
year: '2-digit',
month: 'long'
}).slice(0, -3);
}
console.log(getLocalYearMonth(new Date()));
Варианты с капитализацией строки:
function getLocalYearMonth(d) {
let result = d.toLocaleDateString('ru-RU', {
year: '2-digit',
month: 'long'
});
return result.charAt(0).toUpperCase() + result.slice(1, -3);
}
console.log(getLocalYearMonth(new Date()));
function getLocalYearMonth(d) {
let result = d.toLocaleDateString('ru-RU', {
year: '2-digit',
month: 'long'
}).slice(0, -3);
return result.charAt(0).toUpperCase() + result.slice(1);
}
console.log(getLocalYearMonth(new Date()));
function getLocalYearMonth(d) {
let result = d.toLocaleDateString('ru-RU', {
year: '2-digit',
month: 'long'
}).split('');
result[0] = result[0].toUpperCase();
return result.slice(0, -3).join('');
}
console.log(getLocalYearMonth(new Date()));
Рекомендую первый.
Сборка персонального компьютера от Artline: умный выбор для современных пользователей