1

i have to restart server to get the time updated in IST object.
Development Environment -- "Cloud9".
Following code extract;

var http = require("http");
var fs = require("fs");
var path = require("path");

var files = {};
var port = process.env.PORT;
var host = process.env.IP;

//Convert time to Indian Standard Time
var IST = new Date(); // Clone UTC Timestamp (once stamped, time not updating here)
IST.setHours(IST.getHours() + 5); // set Hours to 5 hours later
IST.setMinutes(IST.getMinutes() + 30); // set Minutes to be 30 minutes later

var assets = function(req, res){
var serve = function(){
res.writeHead(200, {'Content-Type' : 'text/html'});
res.write("day:" + IST.getDay());
res.write("day:"+ IST.getDay() + " " + IST.getHours() + " " + IST.getMinutes() + "<br>");
res.write("server time: "+ (new Date()).getDay() + " " + (new Date()).getHours() + " " + (new Date()).getMinutes());
res.end();

};
serve();

};

var app = http.createServer(assets).listen(port,host);
console.log("Listening on " + host + ":" + port);

Output (before restarting server):

day: 3
day: 3 15 8
server time:  5 3 41
/cse/g1
day: 3
day: 3 15 8
server time:  5 3 48
/cse/g1
day: 3
day: 3 15 8
server time:  5 3 54
/cse/
day: 3
day: 3 15 8
server time:  5 3 55
/cse/g3
day: 3
day: 3 15 8
server time:  5 3 55

when this extract is run seperatly it runs and does not shows error.

The output present above is from the log of the server that i started 2 days earlier.

Today i restarted server and now the Output is:

Ouput (after restarting server):

day: 5
day: 5 10 54
server time:  5 5 25
/cse/
day: 5
day: 5 10 54
server time:  5 5 25
/cse/
day: 5
day: 5 10 54
server time:  5 5 26
/cse/
day: 5 
day: 5 10 54
server time:  5 5 26
/cse/
day: 5
day: 5 10 54
server time:  5 5 26
/cse/
day: 5
day: 5 10 54
server time:  5 5 26
/cse/
day: 5
day: 5 10 54
server time:  5 5 27
/cse/
day: 5
day: 5 10 54
server time:  5 5 29

Preview can be checked at https://nodejs-basic-prashantdawar.c9.io/.
Please refresh after few minutes and note the output every time. you see the ouput as shown above. How to correctly show updated time??

Gist of modified code for simplicity: https://gist.github.com/a36a893a772f3582d0d4.git

0

1 Answer 1

3

Try this

var http = require("http"),
    fs   = require("fs"),
    path = require("path");

var files = {};
var port  = process.env.PORT,
    host  = process.env.IP;

var assets = function (req, res) {

    //Convert time to Indian Standard Time
    var IST = new Date(); // Clone UTC Timestamp (once stamped, time not updating here)
    IST.setHours(IST.getHours() + 5); // set Hours to 5 hours later
    IST.setMinutes(IST.getMinutes() + 30); // set Minutes to be 30 minutes later


    var serve = function () {
        res.writeHead(200, {
            'Content-Type': 'text/html'
        });
        res.write("day:" + IST.getDay() + "<br>");
        res.write("day:" + IST.getDay() + " " + IST.getHours() + " " + IST.getMinutes() + "<br>");
        var serverTime = new Date();
        res.write("server time: " + serverTime.getDay() + " " + serverTime.getHours() + " " + serverTime.getMinutes());
        res.end();
    };

    serve();
};

var app = http.createServer(assets).listen(port, host);
console.log("Listening on " + host + ":" + port);

Notice how the IST variable is declared in the callback function. So every time you have a request, IST variable date will be updated.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. want to upvote , but reputation too low. Sorry for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.