Format date with javascript

Asked

Viewed 210 times

1

I’m developing an application in which I get the posts from a facebook page and need to format the post creation date that I get via json as follows:

2018-01-12T11:25:41+0000

What function should I use to format this date to the Brazilian standard? Ex: 12/01/2018.

  • Try putting part of the code to help.

  • It is not duplicated. Above I explain that this value 2018-01-12T11:25:41+0000 I receive via Json. And not by the new Date();

  • It is a possible duplicate Odair, this question has answers similar to the ones you have here and the goal is almost identical

2 answers

5

Instate the string as a new Date and use the function toLocaleDateString()

var data = new Date('2018-01-12T11:25:41+0000');
console.log(data.toLocaleDateString('pt-BR'));

2


It is possible to pass the locale when using the function toLocaleDateString():

let data = new Date();
console.log(data.toLocaleDateString('pt-BR'));

  • Actually I do not get the date via Date(); This value is already returned to me via Json and I want to change its format to the standard 12/01/2018.

  • The Date object takes parameters, you can pass this string to it as in the Matheus @Odair response

  • 1

    Yes, @Odair we understand, but to be able to use it to make conversions you need to turn it from String (JSON) to Date(), passing as Parameter in new Date(string para transformar)

Browser other questions tagged

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