Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda