Convert data with Javascript?

Asked

Viewed 1,179 times

3

How do I convert the date to this type 02 Ago 2017 for 02/08/2017?

I’m using the vuejs-datepicker and when selecting it comes in this format, and the documentation says to do so...

customFormatter(date) {
      return moment(date).format('dd MMM yyyy');
    }

But using Moment, wanted no Moment, as would do?

  • 1

    The time is the best lib to date you have on the market and you don’t want to use

  • @Otto no, I don’t want to use, I want to use Pure JS

  • 02 Ago 2017 for 02/07/2017? The right thing wouldn’t be 02/08/2017? And its format will always be this, with the abbreviation of the month in pt?

  • @Lucascosta 7 was an error at the time I was typing, and yes, it was set to PT, but I can set it to EN, and will always be abbreviated yes.

  • I think the @Dnick response fits, maybe with some adjustment to change the month position with the day.. but you could comment on the answers that already exist.

1 answer

5


You can create an instance of the type Date in Javascript and using the method toLocaleDateString().

Capture the date in American format:

let data = new Date(Date.parse('Aug 4, 2017'));

To display date in local format:

console.log(data.toLocaleDateString()) //"04/08/2017"

To display in American format:

console.log(data.toLocaleDateString('en-US'))
  • It worked, thanks :D, I doubt, in case I want to convert to the American format, how would? (Just out of curiosity)

  • console.log(data.toLocaleDateString('en-US'))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.