I want to get the blog post date, however, when I use the following, I get the following format: Fri Jan 21 2022 20:09:28 GMT+0000 (Greenwich Mean Time).
I only want, Fri Jan 21 2022
How can I do this?
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const blogSchema = new Schema({
date: {
type: String,
default: date
}
}, {
timestamps: true
});
I'm then just sticking that date into my EJS file as follows
<p class="date"><%= blog.date %></p>
Assuming you want an easy answer, you could just do something like this:
<p class="date"><%= blog.date.match(/^[a-z]{3}\s[a-z]{3}\s\d{2}\s\d{4}/i)[0] %></p>
or, even simpler:
<p class="date"><%= blog.date.slice(0,15) %></p>
These both assume that your date is already a string (I don't use mongo). If it's not, a toString()
after blog.date
should suffice to make them work.
You could also use something like the (excellent) moment.js library for this, but that might be overkill for your use case.