У меня есть лист Google, который принимает даты, а затем использует эту функцию.
function nextDueDate(date1, date2, date3, date4, date5, date6, done1, done2, done3, done4, done5, done6) {
const today = new Date();
const dates = [new Date(date1), new Date(date2), new Date(date3),new Date(date4), new Date(date5), new Date(date6)];
var dones = [done1, done2, done3, done4, done5, done6];
// Completes earlier tasks if subsequent tasks are completed
var dones = dones.map((val,index) => { if (dones.lastIndexOf('x')>=index ) {return 'x'}});
// Filter for dates that are not done
const futureDates = dates.filter((date,index) => !dones[index]);
// Find the earliest date from the filtered future dates
const nextDueDate = futureDates.reduce((earliest, date) => {
return date < earliest ? date : earliest;
}, new Date('2999-12-31')); // initial date far in the future
// If the nextDueDate year is still in the far future then all tasks are done (on no dates)
return (nextDueDate.getFullYear() === 2999) ? 'DELETE' : nextDueDate.toLocaleDateString(); // Returns the next due date in a readable format
}
После возврата даты даты форматируются следующим образом: 2024-06-26
. Но я хочу иметь возможность форматировать их как «Ср, 26 июня». Но когда я пытаюсь изменить форматирование даты, оно остается прежним. Я предполагаю, что он не возвращается как обычный объект даты. Как мне преобразовать его в тип даты, который я затем могу переформатировать?
Изменение последней строки на это вернет формат даты, который вы ищете.
return (nextDueDate.getFullYear() === 2999) ? 'DELETE' : Utilities.formatDate(nextDueDate, "GMT", "EEE dd MMM"); // Returns the next due date in a readable format
Спасибо. Вы мне очень помогли.
То, как вы обращаетесь с
dones
, похоже, предполагает, что даты указаны в отсортированном порядке. Так ли это?