heh
heh

Reputation: 69

Date not able to Format(OOP)

The function that I am creating right now is to format the date form the console log to DDMMYYYY instead of the given format. However, I am getting this error where is says the getDate is not a function.

userDate.getDate is not a function

How should I go about solving this error?

   function formatDate(userDate) {
      let formatted_date = userDate.getDate() + (userDate.getMonth() + 1) + userDate.getFullYear()
      return formatted_date;
    }

    console.log(formatDate("12/31/2014"));

Upvotes: 2

Views: 31

Answers (1)

Fullstack Guy
Fullstack Guy

Reputation: 16908

You are using getDate() on a string reference, you need to convert it first to a Date object:

function formatDate(userDate) {
    userDate = new Date(userDate);
    let formatted_date = `${userDate.getDate()}/${(userDate.getMonth() + 1)}/${userDate.getFullYear()}`;
    return formatted_date;
}
console.log(formatDate("12/31/2014"));

Upvotes: 2

Related Questions