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

77
Visualizações
Edit the createdDate in mongoose

So I have a notice section on my site which displays what date the notice was created. When showing the createdAt date it displays like this -

Thu Mar 03 2022 15:11:22 GMT+0000 (Greenwich Mean Time)

Instead I would like it display as

03/03/2022 - 15:11

Thats DD/MM/YEAR.

Here is my Schema and my EJS that I am displaying it with. Also how it is stored in the database.

Schema

const noticeSchema = new mongoose.Schema({
noticeTitle: {
    type: String,
    required: true
},
noticeText: {
    type: String,
    required: true
},
author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
}
}, {timestamps: true 
}); 

EJS

<% notices.map(notice => notices.createdAt ) %>
<%= notice.createdAt %>

Database Entry

_id: new ObjectId("6220ead4f238fc30822e5e6d"),
noticeTitle: 'test3',
noticeText: '<p>test3</p>',
author: {
  _id: new ObjectId("621e06a6c29dc2273e412537"),
  firstName: 'test',
  lastName: 'test',
  jobRole: 'Other',
  email: 'test',
  username: 'DeneHCAdmin',
  __v: 0
},
createdAt: 2022-03-03T16:20:36.680Z,
updatedAt: 2022-03-03T16:20:36.680Z,
__v: 0
}

Can anybody help?

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

0

It's as easy as splitting the Date into several constants and connecting them together

const creationDate = new Date(),
  dateYear = creationDate.getFullYear(),
  dateMonth = creationDate.getMonth(),
  dateDay = creationDate.getDay(),
  dateHour = creationDate.getHours(),
  dateMinute = creationDate.getMinutes();

const dateFullTime = `${dateMonth}/${dateDay}/${dateYear} - ${dateHour}:${dateMinute}`

console.log(dateFullTime)
// expected output dd/mm/yyyy - hh:hh (if you'd want a zero before the month and day when it's only one character, you would have to make an easy if statement.)
about 4 years ago · Juan Pablo Isaza Relatório

0

const t = new Date();
const date = t.getDate();
const month = t.getMonth();
const year = t.getFullYear();
const hours = t.getHours();
const minutes = t.getMinutes();
const time = `${date}/${month}/${year} - ${hours}:${minutes}`;

It is just simple date manipulation in javascript, doesn't have anything to do with mongoose itself

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