Javascript to get the server time, javascript Server

Source: Internet
Author: User

Javascript to get the server time, javascript Server

JS is a script running on the client, rather than running on the server. In general, the time obtained through JS is the time on the guest's local computer, in order to make the time on the webpage not different from the visitor's computer, we need to directly obtain the server time in JS.

The following is the complete javascript code for getting server time. It is a complete example of getting server time using javascript, and js dynamically refreshes server time.

<! DOCTYPE html> 

Train of Thought Analysis:

The concept of server time is vague. Because the server end may not be composed of one machine after all. The simplest case is the web server and db server. Which one are you talking about?

For web servers.
Then, the new Date () in java or. net is OK.
Do not add new Date in <script> on the webpage. That is only the client time.

For db servers.
You need to connect to the database and obtain it through SQL. For example, oracle is select sysdate from dual.

Example:

There are many methods for js to obtain such server-side time. The general idea is similar.
1.
<Script type = "text/javascript">
Var date = <% = date (the time obtained on the server side and the Content Retrieval Method has been analyzed above) %>
</Script>

2. <script type = "text/javascript" src = ".../ScriptServlet"> </script>
Write a js file into the Servlet response stream so that var nowDate = server-side time

3. ajax. I don't want to talk about it anymore. There are a lot of resources on the Internet.

The above is all the content of this article. I hope you will like it.

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.