OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why can't I format my dates after they are returned from this function?

  • Thread starter Thread starter Alfie Stoppani
  • Start date Start date
A

Alfie Stoppani

Guest
I have a Google sheet which takes dates and then uses this function.

Code:
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
}

After the date is returned, the dates are formatted like this: 2024-06-26. But I want to be able to custom format them as 'Wed 26 June'. But when I try to change the date formatting it stays the same. I assume it's not returning as a normal date object. How do I get it to convert to a date type I can then re-format?

<p>I have a Google sheet which takes dates and then uses this function.</p>
<pre><code>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
}
</code></pre>
<p>After the date is returned, the dates are formatted like this: <code>2024-06-26</code>. But I want to be able to custom format them as 'Wed 26 June'. But when I try to change the date formatting it stays the same. I assume it's not returning as a normal date object. How do I get it to convert to a date type I can then re-format?</p>
 

Latest posts

Top