最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is date() function in node js giving server side date or client side date? - Stack Overflow

programmeradmin9浏览0评论

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?

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?

Share Improve this question edited Jan 29, 2017 at 9:36 Simon Boudrias 44.7k16 gold badges108 silver badges136 bronze badges asked Jan 29, 2017 at 9:28 Bibudha R SahooBibudha R Sahoo 651 silver badge8 bronze badges 1
  • 1 The server one. It's obvious, there can be no client at all. – Wiktor Zychla Commented Jan 29, 2017 at 9:33
Add a ment  | 

4 Answers 4

Reset to default 5

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;

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:

  1. 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.

  2. 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.

The date you get is not the client date (browser related), it returns server side date.

Server side data definitely (just writing because it wont allow me to post)

发布评论

评论列表(0)

  1. 暂无评论