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

136
Views
Node.js: Trying to sort ISO dates with respect to their duration

I'm trying to make a line chart using the chart.js library. I have an array that contains the ISO dates. I'm trying to check who makes ISO dates belong to the same hour and the same day. I would then use this information to create the line chart using the chart.js library.

For example, I have this array of ISO dates:

[
  { date: '2022-06-10T07:32:07.744Z' },
  { date: '2022-06-10T08:32:38.490Z' },
  { date: '2022-06-10T07:32:58.720Z' },
  { date: '2022-06-10T07:33:25.121Z' },
  { date: '2022-06-10T07:35:18.556Z' },
  { date: '2022-06-10T07:35:56.459Z' },
  { date: '2022-06-10T07:36:38.736Z' },
  { date: '2022-06-10T07:36:38.736Z' }
]

I want to know which ISO dates belong to the same hour and the same day from this array.

I'm trying to make a line chart something like this - https://imgur.com/KuLqlD5.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You can use a bit of regex to just get the day and hour part and sort them with that.

const dates = [
  { date: '2022-06-10T07:32:07.744Z' },
  { date: '2022-06-10T08:32:38.490Z' },
  { date: '2022-06-10T07:32:58.720Z' },
  { date: '2022-06-10T07:33:25.121Z' },
  { date: '2022-06-10T07:35:18.556Z' },
  { date: '2022-06-10T07:35:56.459Z' },
  { date: '2022-06-10T07:36:38.736Z' },
  { date: '2022-06-10T07:36:38.736Z' }
]

// Regex to get day and hour
const hourAndDay = new RegExp('(\\d{1,2}T\\d{1,2})');

// Get unique day-hour pairs
const d = dates.map(y => hourAndDay.exec(y.date)[0])
const uniques = dates.filter((x, i) => d.indexOf(hourAndDay.exec(x.date)[0]) === i)

// Result object
const result = {};
uniques.forEach(x => result[hourAndDay.exec(x.date)[0]] = [])

// Sort based on hour and day
dates.forEach((x) => {
    const parsed = hourAndDay.exec(x.date)[0];
    result[parsed].push(parsed.split("T").map(Number))
})
console.log(result)

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!