最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to display Date? - Stack Overflow

programmeradmin2浏览0评论

I am using the following code to display the date in the front-end screen:

<h4> Date :
        <script language="JavaScript">
            date=Date()
            document.write(date)
        </script>
</h4>   

Here h4 is the tag in CSS. Here is the code for h4 in CSS file:

It is displaying the date in local machine. But when I deployed the project to the server machine it is not working.

I am using the following code to display the date in the front-end screen:

<h4> Date :
        <script language="JavaScript">
            date=Date()
            document.write(date)
        </script>
</h4>   

Here h4 is the tag in CSS. Here is the code for h4 in CSS file:

It is displaying the date in local machine. But when I deployed the project to the server machine it is not working.

Share Improve this question edited Nov 8, 2013 at 14:25 BartoszKP 36k15 gold badges107 silver badges134 bronze badges asked Sep 12, 2011 at 9:19 RajRaj 2,49311 gold badges36 silver badges53 bronze badges 4
  • @Raj..You mean you want to display date of server using javascript? – Tushar Ahirrao Commented Sep 12, 2011 at 9:22
  • @Tushar .. Yes , I want to display server time in front end screen using java script – Raj Commented Sep 12, 2011 at 9:23
  • What exactly is your problem? What do you mean by 'it is not working?' If you want to display the time by the server machine, you will have to use a different (server-side) language, such as PHP, because JavaScript is a client-side language. IF you still insist on using javaScript you will have to have a PHP file or something that returns the time on the server, and then get that using JavaScript (AJAX). – Some Guy Commented Sep 12, 2011 at 9:25
  • 1 @Raj, I don't understand why you would do that using javascript. Just display the server time in whatever template you use to generate that h4. In erb, for example, that would be something like "<h4>Date: <%= Time.now %></h4>". Obviously, it will be different depending on what framework you are using on the server. In any case, I don't see why you'd do this with javascript. – Ben Lee Commented Sep 12, 2011 at 9:27
Add a ment  | 

1 Answer 1

Reset to default 3

Javascript runs on the client machine (on the browser). new Date() in javascript gives the date from the client's machine.

To get the date from the server side, you need to pass the date from the server to the client. How you do this depends on the server side technology.

In JSP it would be something like:

<h4> Date : <%= new java.util.Date()%> </h4>

And in PHP:

<h4><?php echo date("d-m-Y"); ?> </h4>
发布评论

评论列表(0)

  1. 暂无评论