by Ian Schoonover
Time passed since date created
Use moment in your show.ejs file
Open your command line (terminal) and install moment js to your project with the following command:
npm i -S moment
*If you're not familiar with the above syntax, it is simply shorthand for:
npm install --save moment
Open app.js and add the following code right above your passport configuration code:
app.locals.moment = require('moment');
*Now moment is available for use in all of your view files via the variable named moment
var mongoose = require("mongoose");
var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
description: String,
cost: Number,
location: String,
lat: Number,
lng: Number,
createdAt: { type: Date, default: Date.now },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
]
});
module.exports = mongoose.model("Campground", campgroundSchema);
var mongoose = require("mongoose");
var commentSchema = mongoose.Schema({
text: String,
createdAt: { type: Date, default: Date.now },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
});
module.exports = mongoose.model("Comment", commentSchema);
<p>
<em>Submitted By <%= campground.author.username %></em>
</p>
<p>
<em>Submitted by: <%= campground.author.username %>, <%= moment(campground.createdAt).fromNow() %></em>
</p>
<span class="pull-right">10 days ago</span>
<span class="pull-right"><%= moment(comment.createdAt).fromNow() %></span>
Congratulations! Your YelpCamp app now has "time since created" functionality
If you have any questions then please create a thread in the Q&A