Alessandro Crotti
Alessandro Crotti

Reputation: 295

Convert date format into MM/DD/YYYY

I have dates that are outputted in this format:

Thursday, October 11, 2018

I would need to convert it into

10/11/2018

but first I'm struggling with getting rid of the day of the week (Thursday). Trying to look at other posts but cannot find something similar. Thanks much in adnvace

Upvotes: 0

Views: 657

Answers (2)

Zeph
Zeph

Reputation: 1728

Javascript can parse the date as supplied.

new Date("Thursday, October 11, 2018").toLocaleDateString("en-US")
// 10/11/2018

Upvotes: 3

cнŝdk
cнŝdk

Reputation: 32145

You need to use Date#toLocaleDateString() method.

Your code will be like this:

var str = "Thursday, October 11, 2018";
let d = new Date(str);
console.log(d.toLocaleDateString("en-US"))

Note:

To get 10/11/2018 from the string October 11, 2018 we need to specify "en-US" as Locale parameter of toLocaleDateString() method so we get the appropriate output, otherwise if you use it without a locale, it may return a wrong output if your time zone is different.

Upvotes: 3

Related Questions