AKKAweb
AKKAweb

Reputation: 3807

NODEJS: Is there an easy way to convert a date from another language like Portuguese to English

Is there a way, using NODE/Javascript, to convert a Date that is in Portuguese to English?

For example I have a Portuguese Date 08/05/2020 which correctly is May 08, 2020 because it is DD/MM/YYYY. However, when I added that to the Date function, it translates to August 05, 2020, which is wrong.

How can I tell NODE/JS that the date being passed is in a different language and format?

Upvotes: 0

Views: 331

Answers (2)

Jack Bashford
Jack Bashford

Reputation: 44125

If it's just Portuguese to English (DD/MM/YYYY -> MM/DD/YYYY as I understand it), then it's super easy to do with string operations:

const date1 = "08/05/2020";

const [b, a, c] = date1.split("/");

const date2 = [a, b, c].join("/");

console.log(date2);
console.log(new Date(date2).toString());

Upvotes: 0

gchandra
gchandra

Reputation: 94

You can use functions in moment.js library to convert between date types.

Look at the different types of formats available in its documentation.

https://momentjs.com/

Upvotes: 2

Related Questions