Home > Blockchain >  Node Js Timestamp of a deletet task
Node Js Timestamp of a deletet task

Time:01-11

I have the following code. I want to display here the timestamp of the deleted task in my index.ejs file.

<% for(var i = 0; i < complete.length; i  ){ %>
        <li><%= complete[i] %> </li>
    <% } %>

in my index.js i installed npm date-format liabrary and i have this code. But i don't get the code running with the deleted time in my ejs file

var format = require('date-format');
//console.log(format(new Date(), 'yyyyMMdd\tHH:mm:ss'));

var date_time = new Date();
console.log(date_time);

var task = [];
var complete = [];

//post route for adding new task
app.post("/addtask", function(req, res) {
    var newTask = req.body.newtask;
    //add the new task from the post route
    task.push(newTask);
    res.redirect("/");
});

app.post("/removetask", function(req, res) {
    var completeTask = req.body.check;
    //check for the "typeof" the different completed task, then add into the complete task
    if (typeof completeTask === "string") {
        complete.push(completeTask);
        //check if the completed task already exits in the task when checked, then remove it
        task.splice(task.indexOf(completeTask), 1);
    } else if (typeof completeTask === "object") {
        for (var i = 0; i < completeTask.length; i  ) {
            complete.push(completeTask[i]);
            task.splice(task.indexOf(completeTask[i]), 1);
        }
    }
    res.redirect("/");
});

i tried to use the date object and to display it with concatination, but it didn't work

CodePudding user response:

I solved my own question. I installed moment and added to index.js

var moment = require('moment');

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

and then i added to my index.ejs file the code.

     <% for(var i = 0; i < complete.length; i  ){ %>
      <li><%= complete[i] %> erledigt am <%= moment().format('DD.MM.YYYY, HH:mm:ss') %></li>
<% } %>
  • Related