Where JavaScript gets the new Date () from

Where does JavaScript get the new date () from?

Is it based on the time settings of the client’s local computers or something else?

I cannot find anywhere only where it is documented. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date

+5
source share
4 answers

Given that JavaScript does not require an Internet connection, it receives the current date and time (and through a proxy, UTC offset / lockout) from the local client environment. You can check this by changing the local clock.

Remember to change it back. :)

+8
source

The JavaScript Date object represents a point in time based on several milliseconds since the Age (January 1, 1970 at midnight UTC). Naturally, new Date uses the clock of the environment in which it starts to get this value. So if it is in the browser on my machine and my clock is not set correctly, it will use my device incorrectly. *

Then they have two sets of functions that you can use to obtain information about this point in time: a local time zone function, such as getHours , getMonth , etc., and UTC functions, such as getUTCHours , getUTCMonth , etc. Local time zone functions work in the time zone of the environment. Naturally, UTC functions work in UTC.

So, let's say someone is in California on March 3, 2017 and does this at 11:30 in the morning exactly at the time:

 var dt = new Date(); console.log(dt.getHours()); // 11 -- eg, 11 am console.log(dt.getUTCHours()); // 19 -- eg, 7 pm 

The base value of the object is 1488569400000, but the functions of the local time zone tell us that 11 hours and the functions of UTC tell us about it at 7 hours.


* (Although, as James Thorpe points out , the specification is a little vague about this, simply saying that it uses "current time", so theoretically the environment can decide whether to use a time server other than the local machine, but ...)

+7
source

Yes, this is based on the local time of the device on which the call is evaluated, for example, in any other language.

+3
source

This is the system date of the client (user). Syntax:

 new Date(); new Date(value); new Date(dateString); new Date(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]]); 

A Date object is a data type embedded in the JavaScript language. Date objects are created with the new Date (), as shown below.

Once a Date object is created, a number of methods allow you to work with it. Most methods simply allow you to get and set the fields of the year, month, day, hour, minute, second, and millisecond of an object using local time or UTC (universal or GMT).

The ECMAScript standard requires a Date object to represent any date and time up to millisecond precision for 100 million days before or after 1/1/1970. This is a range of plus or minus 273,785 years, so JavaScript can represent a date and time of up to 275755 years. Link URL: https://www.tutorialspoint.com/javascript/javascript_date_object.htm

+1
source

Source: https://habr.com/ru/post/1264989/


All Articles