Datetime formatting / customization in EJS Template Engine
Last Updated :
22 Mar, 2024
EJS stands for Embedded JavaScript. It is a template engine designed to be used with server-side JavaScript environments like NodeJS and to generate dynamic content on a web page. Datetime formatting in EJS involves manipulating date and time objects to present them in a desired format within your web application. In this article, we will learn about How to do DateTimeits Formatting in EJS with different different Approaches with it's syntax and example.
We will go through different approaches to Format Datetime in EJS. refer to the example section for examples of all approaches.
Using JavaScript Date Methods:
In this approach, we can directly use JavaScript's built-in Date object and its methods to format datetime values.
Syntax:
<% let date = new Date() %>
<%= date.toDateString() %>
<%= date.getDate() %>
<%= date.getMonth()+1 %>
<%= date.getFullYear() %>
Using Moment.js Library:
In this approach, we are using Moment.js library. It is used to do datetime manipulation and formatting in JavaScript. You can include it in your Node.js project and use it's features for datetime operations.
To use Moment.js, You have to install it by using this command:
npm install moment
Syntax:
const FormattedDate1 = moment().format('MMMM Do YYYY, h:mm:ss a');
const FormattedDate2 = moment().format('dddd');
Steps to Create Node App & Install Required Modules:
Step 1: Create a NodeJS application using the following command:
npm init
Step 2: Install required Dependencies:
npm i ejs express moment
The updated dependencies in package.json file will look like:
"dependencies": {
"ejs": "^3.1.9",
"express": "^4.18.2",
"moment": "^2.30.1"
}
Example: The Below example is demonstrating the Datetime formatting in EJS.
HTML
<!--File path: views/index.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>EJS Datetime Formatting</title>
<style>
h1 {
color: green;
}
</style>
</head>
<body>
<h1>GeeksForGeeks | Datetime formatting in EJS</h1>
<h3>Approach 1: Using JavaScript Date Methods</h3>
<% let date=new Date() %>
<p><b>whole Date & Time:</b>
<%= date %>
</p>
<p>
<b>toDateString() method:</b>
<%= date.toDateString() %>
</p>
<p>
<b>toLocaleDateString() method:</b>
<%= date.toLocaleDateString() %>
</p>
<p>
<b>getDate(), getMonth(), getFullYear() methods:</b>
<%= date.getDate() %>/<%= date.getMonth()+1 %>/<%= date.getFullYear() %>
</p>
<p>
<b>getHours(), getMinutes(), getSeconds() methods:</b>
<%= date.getHours() %>:<%= date.getMinutes() %>:<%= date.getSeconds() %>
</p>
<h3>Approach 2: Using Moment.js Library</h3>
<p>
<b>Formatted Date 1:</b>
<%= FormattedDate1 %>
</p>
<p>
<b>Formatted Date 2:</b>
<%= FormattedDate2 %>
</p>
<p>
<b>Formatted Date 3:</b>
<%= FormattedDate3 %>
</p>
<p>
<b>Formatted Date 4:</b>
<%= FormattedDate4 %>
</p>
</body>
</html>
JavaScript
//File path: /index.js (root)
// Import required modules
const express = require('express');
const path = require('path');
const moment = require('moment');
// Create an Express application
const app = express();
// Define the port for the server to listen on
const port = 3000;
// Set EJS as the view engine
app.set('view engine', 'ejs');
// Set the views directory to 'views' in the current directory
app.set('views', path.join(__dirname, 'views'));
// Define a route to render the EJS template when the root path is accessed
app.get('/', (req, res) => {
// Formatting Datetime using Moment.js
const FormattedDate1 =
moment().format('MMMM Do YYYY, h:mm:ss a');
const FormattedDate2 =
moment().format('dddd');
const FormattedDate3 =
moment().format("MMM Do YY");
const FormattedDate4 =
moment().calendar();
// Render the EJS template named 'index' and pass the data
res.render('index',
{
FormattedDate1,
FormattedDate2,
FormattedDate3,
FormattedDate4
});
});
// Start the server and listen on the specified port
app.listen(port, () => {
// Display a message when the server starts successfully
console.log(`Server is running at http://localhost:${port}`);
});
To run the application use the following command:
node index.js
Output: Now go to http://localhost:3000 in your browser:

Similar Reads
Change the Default Date Format in Django Template We often need to display dates in a format different from the default for the sake of user experience. Django provides several ways to customize the date format directly within templates, allowing us to present dates and times according to our specific requirements. In this article, weâll explore ho
3 min read
React.js Blueprint Date Input Date Formatting Blueprint is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications. In this article, we'll discuss React.js Blueprint Date Input Date formatting. The Date Input component is a type of InputGro
3 min read
React.js Blueprint DateInput2 Date formatting Blueprint is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications. In this article, we'll discuss React.js Blueprint DateInput2 Date formatting. The DateInput2 component is a type of InputGro
3 min read
Moment.js Customize Long Date Formats Locale longDateFormat in Moment.js: The longDateFormat should be passed an object for each long date format, i.e. `L LL LLL LLLL LT LTS`, you want to customize. Before proceeding, please install the moment.js library using the following command. Installation: npm install moment Syntax: moment.update
2 min read
Moment.js Customize Relative Time Moment.js is very easy to customize. In general, you should create a locale setting with your customizations. In this article, we will discuss the moment.js customize relative time in detail with examples. The moment.updateLocale() function allows us to help in obtaining the relative time. It helps
3 min read
Formats for date and dateTime in JSON payloads JSON stands for JavaScript Object Notation. It is a text-based data exchange format that allows you to transfer data between different languages and platforms. JavaScript is commonly used to interact with JSON files. we will explore the Format for date and dateTime in JSON payloads in different ways
3 min read
JSTL Formatting <fmt:formatDate> Tag In developing JSTL applications, we used date and time as one of the representative components. But in some cases, the date or time which is displayed is to be more sorted and in a proper format. So to solve this, we can use the JSTL Formatting <fmt:formatDate> Tag. tag is used to format the t
3 min read
Moment.js Customize Ordinal Names Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates. The ordinals of date values of the Moment output is customizable according to our needs. We will be using the moment.updateLocale() method for this. Syntax: moment.updateLocale('en', { ordinal: callba
2 min read
Moment.js Customize Month Names In this article, we will discuss the moment.js Customize Month Names in detail with examples. Moment.js is very easy to customize. In general, you should create a locale setting with your customizations. The moment.updateLocale() function allows us to add month names to the locale customization. It
2 min read
What is EJS Template Engine ? EJS Template Engine or EJS is a popular template engine for web development. it is used in Nodejs. It allows you to generate dynamic content by embedding JavaScript to HTML content. EJS serves as a templating language, utilizing plan javascript to HTML. It allows the injection of data into the templ
3 min read