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

268
Visualizações
Format nested dates without changing object structure

I am looking for a good clean solution for this. I have an object with dates that can be type Date | null | string:

someObject: {
    array: [
       {
          dateOne: '2021-10-21T22:00:00.000Z',
          dateTwo: '2021-10-21T22:00:00.000Z',
       }
    ];
 };

I am looking for a good solution to apply date formatting to the above without changing its structure. I am aware that the below example would not work, its just an example that would work for nested Dates in an object, but maybe something along these lines. Example below:

someObject: Object.entries(values.someObject).reduce((acc, [a, b]) => {
                  if (b instanceof Date) {
                     return {
                        ...acc,
                        [a]: moment(b).format('YYYY-MM-DD'),
                     };
                  }
                  return acc;
               }, values.someObject),

Expected output:

    someObject: {
    array: [
       {
          dateOne: '2021-10-21',
          dateTwo: '2021-10-21',
       }
    ];
 };

TIA

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

0

You can use split and map here

const obj = {
  someObject: {
    array: [
      {
        dateOne: "2021-10-21T22:00:00.000Z",
        dateTwo: "2021-10-21T22:00:00.000Z",
      },
    ],
  },
};

function getFormattedDate(date) {
  return date.split("T")[0];
}

obj.someObject.array = obj.someObject.array.map(({ dateOne, dateTwo }) => ({
  dateOne: getFormattedDate(dateOne),
  dateTwo: getFormattedDate(dateTwo),
}));

console.log(obj);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */

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

/* Or in the context mentioned above */

someObject: Object.entries(
              values.someObject
           ).reduce((acc, [a, b]) => {
              if (b[0].dateOne instanceof Date) {
                 return {
                    ...acc,
                    [b]: a.map(({ dateOne, dateTwo }) => ({
                       dateOne: moment(dateOne).format('YYYY-MM-DD'),
                       dateTwo: moment(dateTwo).format('YYYY-MM-DD'),
                    })),
                 };
              }
              return acc;
           }, values.someObject),
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