0

I am using the date object below to obtain the current time from the user:

    var currentdate = new Date();
    return (currentdate.getDate() + "/"
            + (currentdate.getMonth()+1)  + "/"
            + currentdate.getFullYear() + " @ "
            + currentdate.getHours() + ":"
            + currentdate.getMinutes() + ":"
            + currentdate.getSeconds());

My question is where does currentdate obtain the time from? Does it grab the time from a central server? Would any user from anywhere around the globe execute the codes above at the same time will obtain the same value?

Or this is per user based, meaning if two users from different time zones execute the codes at the same time, they will obtain the time based on their time zone, hence the codes return different values?

1
  • 4
    Date is based on the user's machine time. Commented Jan 6, 2020 at 16:47

6 Answers 6

3

In the code you provided, the first line is:

var currentdate = new Date();

The constructor of the Date object is called without any parameters, and thus is initialized with a timestamp having the same value as Date.now(). That value is a Unix timestamp, containing milliseconds since 1970-01-01 00:00:00.000 UTC (not accounting for leap seconds). It is strictly UTC based. The sytem time zone is not involved in the construction, and the Date object does not store the system time zone.

Only the system clock is involved. Every computer system has an internal clock that can return the current UTC time. This clock is most often synchronized with clocks of other computers via NTP, PPTP, or similar mechanisms. It is computer dependent though. For example, when configured for "set time automatically", Windows computers will obtain their time either from a public NTP server on the Internet, or from a domain controller on a local network, depending on the network configuration. However, a standalone computer that is not configured to set time automatically, could have a very different clock value.

Thus, two given computers calling new Date() (or Date.now()) will likely retrieve similar values if their clocks are synchronized correctly, but are not guaranteed to. There is no network call involved, only a call to the local system clock.

In the rest of your code, you call the functions getMonth, getFullYear, getHours, getMinutes, and getSeconds. Each of these function calls will independently use the Date object's UTC-based timestamp and the time zone configured on the computer where the code is being executed. If the code is executed in a web browser, then it's the user's computer's time zone that is applied. If the code is executed on the server, then it's the server's time zone that is applied.

Again, the important thing to remember is that Date object only track a single numeric value - the milliseconds since the Unix epoch (not accounting for leap seconds). You can see this value directly by calling d.getTime() or d.valueOf() on the Date object (named d here), or by coercing the value to a Number as in +d. The time zone is not part of the object's state, and it does not make network calls on its own.

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

Comments

2

This is based on the Unix timestamp and will be based on the users' computers time if requested client-side, or the servers timestamp if requested server-side.

For further reading see:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

Comments

0

First of all its a very good question. Lot of developers use date but never bother to go as to how it works exactly. Yes, it's machine based on which the JavaScript engine is running, so yes it depends upon timezone too. See : https://javascript.info/date

Comments

0

According to mdn, the exact date/hour is based on your computer. More details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours

Comments

0

Javascript runs on a web browser so it will take the time form the user's machine. So if your users sit in different time zone then they will see different time.

Comments

0

Javascript Date is executed on the client side ! So yes each user would see his machine time and his specific Timezone. Even when the user has a wrong time in his machine, he would see it wrong.

The exception returns Server time when you are using Node.js for example. In that case, local time would be the server time. Again on servers, usually time and Timezones follow the OS time setting.

If you expect them all to see the same time, Like displaying : "Time in London 10:13pm"

You need to store your server time in a javascript variable using your server' programming language.

Comments

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.