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

138
Vistas
Change the format of date stored in MongoDB to display on the front end

So I have posts section on my site where users can post to the front page, I want to display on the page the date that each post was added. I have added a notice created line to my Schema below.

Post Schema

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

I am then displaying the noticeCreated date on the front end with EJS

Displaying with EJS

<%= notices.noticeCreated %>

The problem is its displaying like this

Fri Mar 04 2022 14:51:10 GMT+0000 (Greenwich Mean Time)

I Want it to display like this

04/03/2022

I must of spent 5+ hours trying to find a method to fix this but I can't find anything that's actually helped me. Obviously its just displaying how its stored in the mongoDB database, but is there a way i can change this?

Stored in the database like this

 noticeCreated: 2022-03-04T14:51:10.927Z,

Also here is my route if that helps.

router.get('/:id', isLoggedIn, wrapAsync (async (req, res, next) => {
const { id } = req.params;
const notices = await Notice.findById(id).populate('author');
console.log(notices)
if (!notices) {
req.flash('error', 'This Notice Does Not Exist')
res.redirect('/notice')   
}
res.render('notices/show', { notices })
}))
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

  <div class="card-subtitle text-muted mb-2">
            <%= article.createdAt.toLocaleDateString() %>
  </div>

I use this in my frontend to store date as you mention. See Here

about 4 years ago · Juan Pablo Isaza Denunciar

0

We can do this by the following example

var test= "1993-06-15T00:00:00.000+00:00"

alert( new Date(test).toLocaleDateString())
about 4 years ago · Juan Pablo Isaza 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