Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
how can group UTC dates by date YYY-MM-DD?

I have an array of utc dates like:

[
 {createdAt: '2022-01-19T22:15:33.008Z'},
 {createdAt: '2022-01-19T21:15:33.008Z'},
 {createdAt: '2022-01-19T10:15:33.008Z'},
 {createdAt: '2022-01-20T23:15:33.008Z'},
 {createdAt: '2022-01-21T23:15:33.008Z'},
 {createdAt: '2022-01-22T23:15:33.008Z'},
 {createdAt: '2022-01-18T23:15:33.008Z'},
]

My desired output is:

{
  "2022-01-19":[
    {createdAt: '2022-01-19T22:15:33.008Z'},
    {createdAt: '2022-01-19T21:15:33.008Z'},
    {createdAt: '2022-01-19T10:15:33.008Z'}
  ],
   "2022-01-20":[
    {createdAt: '2022-01-20T23:15:33.008Z'},
  ],
   "2022-01-21":[
    {createdAt: '2022-01-21T23:15:33.008Z'},
  ],
   "2022-01-22":[
   {createdAt: '2022-01-22T23:15:33.008Z'},
  ],
   "2022-01-18":[
    {createdAt: '2022-01-18T23:15:33.008Z'},
  ],

}

I don't know if there is any date management function to get the YYYYY-MM-DD of a timestamp. how can I do it?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

let tab = [
 {createdAt: '2022-01-19T22:15:33.008Z'},
 {createdAt: '2022-01-19T21:15:33.008Z'},
 {createdAt: '2022-01-19T10:15:33.008Z'},
 {createdAt: '2022-01-20T23:15:33.008Z'},
 {createdAt: '2022-01-21T23:15:33.008Z'},
 {createdAt: '2022-01-22T23:15:33.008Z'},
 {createdAt: '2022-01-18T23:15:33.008Z'},
]
let output = {};
tab.forEach(element => {
  let date = element.createdAt.split('T')[0];
  if (output[date] == undefined) {

    output[date] = []
  }
  output[date].push(element)
});

console.log(output)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can split the string by the character 'T', then get the first item in the array to convert the string into the correct format.

Then, you can use Array.reduce to construct the object:

let arr = [
 {createdAt: '2022-01-19T22:15:33.008Z'},
 {createdAt: '2022-01-19T21:15:33.008Z'},
 {createdAt: '2022-01-19T10:15:33.008Z'},
 {createdAt: '2022-01-20T23:15:33.008Z'},
 {createdAt: '2022-01-21T23:15:33.008Z'},
 {createdAt: '2022-01-22T23:15:33.008Z'},
 {createdAt: '2022-01-18T23:15:33.008Z'},
]

const res = arr.reduce((a,b) => {
  let date = b.createdAt.split("T")[0];
  if(!a[date]){
    a[date] = [];
  }
  a[date].push(b);
  return a;
}, {})

console.log(res)
.as-console-wrapper {
  max-height: 100%!important;
  top: 0
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda