-2

1 - Make a function that given a date in text format "dd/mm/yyyy" returns a Date object with that date, using the Split() method.

2 - You have to do a "console.log() of the Date object created" and it should output something similar to

Mon Dec 2 2019 11:36:25 GMT+0100 (Central European Standard Time).

I have tried this but I don't know why when I write a console.log

I get the following: 2022-12-05T00:00:00.000Z

And I try to get it to appear something like this:

Mon Dec 2 2019 12:30:05 GMT+0100 (Central European Standard Time)

function convertDate(date) {
    let dateArray = date.split("/");
    let DateString = dateArray[2] + "/" + dateArray[1] + "/" + dateArray[0] + "Z";
    let dateDate = new Date(dateString);
    return dateDate;
}

console.log(convertDate("05/12/2022"));
marioconde
  • 17
  • 5
  • Use `console.log(dateDate.toString())`. – Heiko Theißen Dec 28 '22 at 11:48
  • '*we must first change the format of the date and then add a Z to the end: “2022/12/01Z”*'. That is a very bad idea, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Dec 28 '22 at 12:56

1 Answers1

-1

you can use new Date() to get the date like "Wed Dec 28 2022 11:36:25 GMT+0100"

let date = new Date("05/12/2022");
console.log(date);

output will be Thu May 12 2022 00:00:00 GMT+0500 (Pakistan Standard Time) {}

Zanaen
  • 21
  • 4
  • See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Dec 28 '22 at 12:58