Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140788

How can I add the date to my posts in Javascript?

$
0
0

I am making a blog using JavaScript, Express, and ejs templates. With each blog post there is the ability to post a comment, and I would like to have the date posted with each comment. The content of the comments are values being pulled from input fields in an .ejs file. Right now I am using moment.js, but the date/time keeps updating. I would like it to only show the date/time the comment was created.

Here is some code from my app.js file:

   moment = require('moment');
   app.locals.moment = moment 

This my route for creating the comment:

app.post("/comments/:id", (req, res) => {
  Blog.findById(req.params.id, (err, blog) => {
    if (err) {
      console.log("Something went wrong:", err);
    } else {
      console.log("BLOG", blog);
      let newComment = { comment: req.body.comment };
      Comment.create(newComment.comment, (err, comment) => {
        if (err) {
          console.log("Something went wrong:", err);
        } else {
          console.log("Success!  Comment Posted", comment);
          comment.save();
          blog.comments.push(comment);
          blog.save();
          res.redirect("/blog/" + blog._id);
        }
      });
    }
  });
});

And from my .ejs file:

<em><%= moment(Date()).format(shortDateFormat) %></em>

I think this should be enough to get the gist of it. This is probably a really easy fix but I'm still a pretty green coder!


Viewing all articles
Browse latest Browse all 140788

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>