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 })
}))
We can do this by the following example
var test= "1993-06-15T00:00:00.000+00:00"
alert( new Date(test).toLocaleDateString())