JavaScript implementation to get server time _javascript tips

Source: Internet
Author: User

JS is run on the client script, rather than running on the server, usually, through JS acquisition time when the time is the local computer visitors time, in order to make the page on the time not because of the difference between the visitors computer, we have to direct JS access to the server time.

The following is the complete JS get the server time code, is to use JavaScript to get the full example of server time, JS dynamic refresh server time.

<! DOCTYPE html>  

Thinking Analysis:

The concept of server-side time is vague. Because the server side may not be made up of a single machine after all. In the simplest case, the Web server and the DB server are also divided. What kind of a word are you talking about?

If it is a Web server.
So the direct in Java or the. NET new Date () is OK
Do not new Date in <script> inside the page, that is only the client time

If it is a DB server.
You need to connect to the database and get it through SQL. Oracle, for example, is the select Sysdate from dual.

Example:

If JS to get such a server-side time, there are many ways. The idea is roughly the same.
1.
<script type= "Text/javascript" >
var date = <%=date (the time that has been obtained on the server side, the content obtained method has been analyzed above)%>
</script>

2.<script type= "Text/javascript" src= ".../scriptservlet" ></script>
Writes the servlet's return stream to a JS file in which the var nowdate = server-side time

3.ajax, this is not said, online a lot of

The above mentioned is the entire content of this article, I hope you can enjoy.

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.