Reputation: 65
I am new to Node JS. If I write Date()
in node.js will it give server side date or it acts like its original javascript behavior as javascript Date()
gives client PC date. So in Node JS how will it perform its task?
Upvotes: 2
Views: 4340
Reputation: 1074485
The JavaScript Date
constructor with no arguments:
var dt = new Date();
...returns a Date
instance for the current date/time in the environment where it's run. So if it's running on a server (or workstation) in NodeJS, it'll give the current date/time according to that server or workstation. If it's run in JavaScript executing on a client machine (perhaps in a web browser), it'll return the date/time according to that client machine.
Since Date
represents a specific moment-in-time, the only real differences are:
If you're running NodeJS on your own server, you can ensure that the server's clock is correct, and so trust the date/time you get. Whereas you have no idea whether the client's clock is correct.
The Date
object only understands UTC and "local" time. UTC is obviously the same everwhere, so there's no difference server vs. client, but local time obviously depends on timezone, and the timezone of a server is frequently different from the timezone of a client.
Date.now()
returns a number, which is the number of milliseconds since "The Epoch" (January 1st 1970 at midnight UTC). So only #1 above applies to that number.
Upvotes: 2
Reputation: 32767
If the date is processed by Node.js, results will be from server-side. You can clearly notice this by the fact that the client will not see date
but rather the date result.
Server-side date processing (app.js):
response.render("home", {now: Date.now()});
If it's loaded on the client side, for example, the code residing on an Node.js HTML template, then the date results will be from the client.
Client side processing (index.html):
var now = Date.now();
document.getElementById('now').innerHTML = now;
Upvotes: 5
Reputation: 4853
The date you get is not the client date (browser related), it returns server side date.
Upvotes: 1
Reputation: 4735
Server side data definitely (just writing because it wont allow me to post)
Upvotes: 0