Как вывести последний день месяца и сам месяц текстом?
Как с помощью jquery вывести последний день месяца, и сам месяц?
Например:
сейчас 23.02.2021 - выводим 28 февраля; сейчас 10.03.2021 - выводим 31 марта.
Ответы (2 шт):
Автор решения: Алексей Шиманский
→ Ссылка
let date = new Date();
let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log(firstDay.getDate(), lastDay.getDate());
//----------------------------------------------------//
let month = 1; // Февраль
let year = 2020;
date = new Date(year, month + 1, 0);
firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log(firstDay.getDate(), lastDay.getDate());
/* сейчас 23.02.2021 - выводим 28 февраля; сейчас 10.03.2021 - выводим 31 марта.*/
date = new Date(2021, 1, 23);
lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log(lastDay.getDate());
date = new Date(2021, 2, 10);
lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log(lastDay.getDate());
По поводу названия месяца, есть ответ: https://ru.stackoverflow.com/a/721536/191482
Автор решения: Konstantin Modin
→ Ссылка
С jQuery не знаю с moment можно примерно так:
const d1 = moment("23.02.2021", "DDMMYYYY")
.endOf("month")
.toDate()
.toLocaleDateString("ru", { day: "numeric", month: "long" });
const d2 = moment("10.03.2021", "DDMMYYYY")
.endOf("month")
.toDate()
.toLocaleDateString("ru", { day: "numeric", month: "long" });
console.log("d1", d1);
console.log("d2", d2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>