Display Current Day Name in Javascript – Node, Express, EJS

Print present day’s name like Sunday, Monday in javascript app. Display the day name from the date library. In this post we also share a sample Express JS app for displaying the day, EJS library is used for templating.

    var today = new Date();
    var day = today.toLocaleString('en-us', {weekday: 'long'});;
    console.log(day);

Day Printing – Express JS APP

app.js

const express = require("express");
const bodyParser = require("body-parser");

const app = express();

app.set('view engine', 'ejs');

app.get("/", function(req,res){

    var today = new Date();
    var day = today.toLocaleString('en-us', {weekday: 'long'});;
    console.log(day);
    res.render("list", {dayName: day});
});

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

views/list.ejs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Current Day</title>
</head>
<body>

    <h1> It's a <%= dayName %>!</h1>
    
</body>
</html>

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published.