Search code examples
javascriptnode.jsexpresssend

How to use res.send method after clearInterval()?


I have to make continuous output to the server console of the current date and time. I have to stop output to the console after the specified time and return the current date and time to the user. How should I use ClearInterval() correctly?

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

let timerId = setInterval(() => console.log(new Date().toUTCString()), 1000);

const r = app.get('/', (req, res) => {
    let lastDate = clearInterval(timerId);
    res.send(lastDate);
});

app.listen(3000);

Solution

  • You can simply use res.send(new Date().toUTCString())

    And to add +2 hours to your current time you can do

    var date = new Date();
    date.setHours( date.getHours() + 2 );
    var newDate = date;
    res.send(newDate)