JavaScript how to get server time in detail _javascript tips

Source: Internet
Author: User
Tags time and date

The example in this article describes how JavaScript obtains server time. Share to everyone for your reference, specific as follows:

JavaScript is run on the client script, we generally use new Date () to get the current time, but get the client time, the client time is arbitrarily changed, if you want to do a product release countdown, the client time to change, will fool. Business needs to use the server time of the scene there are many, then only through JS how to get the server time? In fact, only one Ajax request is required, and the current server time is obtained by reading the timestamp inside the response header of the XMLHttpRequest Object!

The principle is so simple: send a request via AJAX to the server, and when the server receives the request, it can read the timestamp of the response head, whether the request succeeds or fails, and gets the timestamp. How to judge the server received the request? When the AJAX request is sent, the XMLHttpRequest has a 5-state change:

Xmlhttprequest.readystate value The meaning of the expression
0 Uninitialized, a XMLHttpRequest object has been created , but has not yet been initialized
1 not sent, the code has called the XMLHttpRequest Open () method and XMLHttpRequest is ready to send a request to the server
2 has been sent, a request has been sent to the server by the Send () method, but no response has been received to read the response header information.
3 being received, HTTP response header information has been received, but the message body part has not been fully received
4 has been loaded, The response has been fully received

By listening to the XMLHttpRequest ReadyStateChange events to determine which state is currently in, from the table can be seen, when the xmlhttprequest.readystate value of 2 o'clock can read the response head to get the time stamp we want. The code is as follows:

<p id= "Time" ></p>
<script>
Ajax ()
 function ajax (option) {
 var xhr = null;
 if (window. XMLHttpRequest) {
  xhr = new window. XMLHttpRequest ();
 } else{//IE
  XHR = new Activeobject ("Microsoft")
 }
 //Request the current file
 xhr.open ("Get", "/") in the way of get;
 Xhr.send (null);
 Listener request state Change
 Xhr.onreadystatechange = function () {
  var time = null,
   curdate = null;
  if (xhr.readystate===2) {
  //Gets the timestamp time
  = Xhr.getresponseheader ("Date") in the response head;
  Console.log (Xhr.getallresponseheaders ())
  curdate = new Date (time);
  document.getElementById ("Time"). InnerHTML = "Server Times is:" +curdate.getfullyear () + "-" + (Curdate.getmonth () +1) + "-" + Curdate.getdate () + "" +curdate.gethours () + ":" +curdate.getminutes () + ":" +curdate.getseconds ();
  }
}} </script>

Here, the server time is taken. In fact, the information in the response header is not only a timestamp, you can use Xhr.getallresponseheaders () to obtain the entire response header information, the response header also includes the server type and version number, the requested file type and coding and other information (see figure I below), Can be read in this way. In order to prove that the acquisition is indeed the server time, I used a mobile phone test (see figure II below), the current Beijing time is April 20, 2015 21:59,PC machine time I transferred to February 1, 2015 21:16

Figure I

Figure II

More readers interested in JavaScript-related content can view the site topics: "JavaScript time and date Operation skills Summary", "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills summary", " JavaScript animation effects and tips Summary, "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and Skills summary" and "JavaScript Mathematical operation Usage Summary"

I hope this article will help you with JavaScript programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.