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