How to Convert MongoDb Date with MomentJS on EJS Templating Engine

MaryShodiyaMaryShodiya
2 min read

In this tutorial, we will discuss the moment library and its importance in converting MongoDb date string format. We will go ahead and insert the data in an EJS template file.

Let's begin first by knowing the use of MomentJS

Moment.js is a javascript library used in parsing, manipulating and validating date and time in Javascript. It was designed to work easily on the browser and Node.js environment.

On Node.js, it can be installed using npm by running the npm install moment command and requiring it in the server.js file

var moment = require ("moment")

moment().format()

On the browser, it can be included inside the script tag as a javascript file

<script src="moment.js" type="text/javascript"></script>
<script>
    moment().format();
</script>

The Moment.js library not only supports the conversion of MongoDB dates but other dates and time libraries. You can check out the documentation for more details.

Prerequisites

Before we proceed, you need to have the following

  1. Basic knowledge of Web Development.

  2. The supported version of Node.js installed on your computer.

  3. A MongoDB Instance running on your machine. If you are using MongoDB Atlas you won't be needing this.

  4. Some basic knowledge of Node.js and Express.js.

  5. A basic understanding of making HTTP requests in Express.

Step 1: Create a new folder by running the following commands on your terminal

mkdir convert_date
cd convert_date

Step 2: Installing the relevant node packages on a Node.js environment

Then install Express, Moment and EJS by running the following command on your terminal.

npm install express moment ejs --save

Step 3: Connect Express and Moment to your Server.

In your server.js file

const express = require("express")
const app = express()
const moment = require("moment")

//using EJS template for view
app.set("view engine", "ejs");

//set up route 
app.get("/", (req, res) => {

//call mongodb date 
  const mongodate = new Date();

//pass moment.js into date 
  const convertedDate = moment(mongodate).format("MMMM Do YYYY, h:mm:ss a"); 

// or to convert date to hours from Now
const convertedDate = moment(mongodate).fromNow()


  res.render("index", { convertedDate });
});

app.listen(3000, () => {
  console.log("Server started on port 3000");
});

Step 3: Then create an index.ejs file and pass the converted property as a variable in your index.ejs file

touch index.ejs
<h1> The converted date is : <%= convertedDate %> </h1>

And you are done!

You can follow me on Twitter and feel free to DM if you have any question.

11
Subscribe to my newsletter

Read articles from MaryShodiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

MaryShodiya
MaryShodiya

I am a Frontend Engineer from Lagos Nigeria who loves to write and document anything tech-related, contribute to open source projects. You can check out my portfolio https://marysho.netlify.app