Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!