computed: {
deadlineText() {
const today = new Date().setHours(0, 0, 0, 0);
const deadline = new Date(this.task.deadline).setHours(0, 0, 0, 0);
return [ 'Уже было', 'Сегодня', 'Жди' ][1 + Math.sign(deadline - today)];
},
},
function getDates(startStr, count) {
const dates = [];
const date = new Date(startStr.split('.').reverse().join('-'));
const day = date.getDate();
date.setDate(0);
while (dates.length < count) {
date.setMonth(date.getMonth() + 2, 0);
date.setDate(Math.min(date.getDate(), day));
dates.push(date.toLocaleDateString('ru-RU', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
}));
}
return dates;
}
function getWeekdaysOfMonth(year, month) {
const date = new Date(year, --month, 1);
const result = [];
while (date.getMonth() === month) {
result.push(date.toLocaleString('ru-RU', {
month: 'long',
day: 'numeric',
weekday: 'long',
}));
date.setDate(date.getDate() + 1);
}
return result;
}
const weekdaysOfDecember2020 = getWeekdaysOfMonth(2020, 12);
но как поступить если я не хочу забирать дни недели из стандартного объекта. а взять из их своего массива?
const weekdays = [
'воскресенье',
'это понедельник',
'а это вторник',
'конечно же среда',
'четверг',
'пятница - прямо после четверга',
'суббота, рабочая неделя окончена',
];
const getWeekdaysOfMonth = (year, month) => Array.from(
{ length: new Date(year, month--, 0).getDate() },
(n, i) => {
const d = new Date(year, month, i + 1);
return d.toLocaleString('ru-RU', {
month: 'long',
day: 'numeric',
}) + ', ' + weekdays[d.getDay()];
});
const weekdaysOfFebruary2021 = getWeekdaysOfMonth(2021, 2);
moment.locale('ru');
const formatDate = dateStr => moment(dateStr, 'YYYY.MM.DD').format('D MMMM YYYY');
data: () => ({
dateStr: '2020.06.01',
}),
computed: {
formattedDate() {
return formatDate(this.dateStr);
},
},
methods: {
formatDate1: formatDate,
},
filters: {
formatDate2: formatDate,
},
<div>{{ formattedDate }}</div>
<div>{{ formatDate1(dateStr) }}</div>
<div>{{ dateStr | formatDate2 }}</div>
Array.from({ length: 48 }, (n, i) => {
const d = new Date(0, 0, 0, 0, 30 * i);
return d.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' });
})
function getDatesGroupedByWeekday(year, month) {
const d = new Date(`${month} 1, ${year}`);
const iMonth = d.getMonth();
const result = {};
while (d.getMonth() === iMonth) {
const date = d.getDate();
const weekday = d.toLocaleString('en-US', { weekday: 'long' });
(result[weekday] = result[weekday] ?? []).push(date);
d.setDate(date + 1);
}
return result;
}
data: () => ({
date: new Date(),
}),
filters: {
formatDate1: d => d.toLocaleString('ru-RU').replace(',', '').slice(0, -3),
},
methods: {
formatDate2: d => d.toLocaleString('ru-RU').replace(',', '').slice(0, -3),
},
<div class="fullDate">{{ date | formatDate1 }}</div>
<div class="fullDate">{{ formatDate2(date) }}</div>
function getDateInTimeZone(utcOffset, date = new Date()) {
const utcTime = date.getTime() + date.getTimezoneOffset() * 60000;
return new Date(utcTime + utcOffset * 3600000);
}
const moscowDate = getDateInTimeZone(3);
const newYorkDate = getDateInTimeZone(-5);
const tokyoDate = getDateInTimeZone(9);
почему 14 часов превратились в 02
как этого избежать?
[...Array(12)].map((n, i) => new Date(0, i).toLocaleString('en-US', { month: 'long' }))
после 21 итерации выдает неправильное значение даты
const startDate = new Date(2019, 7, 11);
const currentDate = new Date(startDate);
for (let i = 0; i <= 30; i++) {
currentDate.setDate(currentDate.getDate() + 1);
console.log(currentDate);
}
const startDate = new Date(2019, 7, 11);
for (let i = 0; i <= 30; i++) {
const currentDate = new Date(startDate);
currentDate.setDate(startDate.getDate() + i);
console.log(currentDate);
}
function getDatesIntervalStr(dates) {
const dateParts = d => ({ year: d.year(), month: d.format('MMMM') });
const start = dateParts(dates[0]);
const end = dateParts(dates[dates.length - 1]);
if (start.year !== end.year) {
return `${start.month} ${start.year} - ${end.month} ${end.year}`;
} else if (start.month !== end.month) {
return `${start.month} - ${end.month} ${end.year}`;
} else {
return `${end.month} - ${end.year}`;
}
}
<input type="date" v-model="years.start">
<input type="date" v-model="years.end">
<div v-for="n in months">
<h3>{{ n.year }}, {{ n.name }}</h3>
<div>{{ n.days }}</div>
</div>
data: () => ({
years: {
start: null,
end: null,
},
}),
computed: {
months() {
const start = moment(this.years.start).startOf('month');
const end = moment(this.years.end).endOf('month');
const months = [];
for (; start < end; start.add(1, 'day')) {
const [ year, name, day ] = start.format('YYYY.MMMM.D').split('.');
if (day === '1') {
months.push({
year,
name,
days: [],
});
}
months[months.length - 1].days.push(+day);
}
return months;
},
},
moment.locale('ru');
document.querySelector('#load').addEventListener('click', function load() {
fetch('https://api.github.com/users/fristyr/repos')
.then(r => r.json())
.then(data => {
const formatIn = 'YYYY-MM-DDTHH:mm:ss';
const formatOut = 'DD MMMM YYYY';
document.querySelector('.repo-wrapp').insertAdjacentHTML('afterbegin', data.map(item => `
<article class="repo">
<a href="${item.html_url}" class="repo__name">${item.name}</a>
<br>
<span class="repo__technology">${item.language}</span>
<span class="repo__update">${moment(item.updated_at, formatIn).format(formatOut)}</span>
</article>`
).join(' '));
});
});