1

my Task is so simple but I need to know how to do it with just js,

I have this date 2021-05-06 I want to convert it to this, Fri 7 May.

how to do this?

william david
  • 69
  • 1
  • 8
  • what is this date `**2021-05-06**` a string with twice stars around ? https://stackoverflow.com/help/how-to-ask and what did you try ? there several chapters about date formating on mdn doc,, did you read them ? – Mister Jojo May 07 '21 at 02:17
  • thanks for your reply, I'm sorry for twice stars its wrong from me, I edited the question – william david May 07 '21 at 02:28

1 Answers1

0

You can do it like this:

const convertDate = (date_string) => {
  let date = new Date(date_string)
  return `${[ 'Sun', 'Mon','Tue','Wed','Thu', 'Fri','Sat'][date.getDay()]} ${date.getDate()} ${['January', 'February', 'March', 'April', 'May','June','July', 'August', 'September', 'October', 'November', 'December'][date.getMonth()]}`;
}

console.log(convertDate('2021-05-06'))
NeNaD
  • 18,172
  • 8
  • 47
  • 89
  • Where the host has a negative timezone offset, that will present values for the previous day, i.e. 5 May, not 6 May, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG May 07 '21 at 04:37