Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Transform string date to Date object in JS

My input is get via an API, and I have this array with the following date format as strings:"2022-06-29T15:30:00+00:00", "2022-07-01T09:00:00+00:00", "2022-07-05T16:00:00+00:00".

I want to transform those date in another format (ex for the first one: 29.06.2022 (DD.MM.YYYY)).

Also, I want to compare dates to sort the array. How can I do this? Do I need to convert it into a Date object? (vanilla JS, no frameworks).

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

You can first sort the dates using sort method

dateArr.sort((a, b) => new Date(a) - new Date(b))

and then map over the array

const dateArr = [
    '2022-06-29T15:30:00+00:00',
    '2022-07-01T09:00:00+00:00',
    '2022-07-05T16:00:00+00:00',
];

const result = dateArr
    .sort((a, b) => new Date(a) - new Date(b))
    .map((str) => str.split('T')[0].split('-').reverse().join('-'));

console.log(result)

about 4 years ago · Santiago Gelvez Report

0

You can consider doing it like this:

const str = "2022-06-29T15:30:00+00:00";

const date = new Date(str);

console.log(date); // 👉️ Wed Jun 29 2022 22:30:00

As for sorting array based on dates, you can take a look at How to sort an object array by date property.

about 4 years ago · Santiago Gelvez Report

0

let dateStr = "2022-06-29T15:30:00+00:00";
let getDate = dateStr.split('T')[0];
const date = new Date(getDate);
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!