0

I'm trying to convert javascript date to string but it's not working, i have a date like 2023-07-05 How to change it to 27 march 2023 please help me

  • 2
    You can use Intl APIs to do that. Please lookup MDN docs – pavi2410 May 07 '23 at 12:45
  • 1
    Specifically at MDN, [toLocaleDateString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString) – Pointy May 07 '23 at 12:46
  • See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse for Date related questions of that kind. – ak.leimrey May 07 '23 at 12:47

1 Answers1

2

You can make use of Date Object and it's functions to achieve this task.

// Online Javascript Editor for free
// Write, Edit and Run your Javascript code using JS Online Compiler

// Create a new Date object with the original date
let date = new Date('2022-02-12'); // add any date here

let day = date.getDate();
let month = date.getMonth();
date.setMonth(month);

// Format the date as a string as required here (DD Month YYYY)
let newDate = day + ' ' + date.toLocaleString('default', { month: 'long' }) + ' ' + date.getFullYear();
console.log(newDate);

Sarthak
  • 380
  • 7