AGeek
AGeek

Reputation: 5225

HTML/JSP Doubt, Print date on webpage

I made a function which displays date on the webpage,, and i uploaded the same onto some server...

But when i changed the date of my system, i noticed that the date is dependent on client machine..

Is it not possible to get the date from actual time server and embed that code into my program..

Hope i am able to explain my doubt.. Please give some solution to this with small sample code..that would be helpful..

Thanks a lot..

Upvotes: 0

Views: 2605

Answers (4)

Tom
Tom

Reputation: 45104

If you have written it in javascript, well...that always executes on the client side. If you are calculating date through javascript, its too late, that code is gone.

To solve this, you would have to make your js function receive data through parameters, and that data should be calculated on the server side.

You could do something like.

<%@ page import ="java.util.Date" %><%--Imports date --%>
<% Date date = new Date();
   String strdate = date.toString();//could be formatted using SimpleDateFormat.
%>

  <!--must be inside a form -->  
  <input type="text" value="javascript:showDate('<%=strdate%>');"/>

  <!--must be inside a table-->
  <td>javascript:showDate(<%=strdate%>);</td>

Or more elegantly, get server date in your java class, and write it to request:

//formattedDate is defined above, in the format you like the most. Could be a 
//java.util.date or a String
request.setDate("date",formattedDate);

And then, in your jsp, using for example, JSTL

<c:out value="${formattedDate}"/>

Or,

<% //this java code is run on the server side.
    String strdate = (String)request.getAttribute("date"); 
 %>
<%=strdate%><!-- prints strdate to jsp. Could put it in a table, form, etc -->

EDIT: In response to your comment, you should:

<%--Imports java packages --%>
<%@ page import ="java.util.Date" %>
<%@ page import ="java.text.SimpleDateFormat"%>

<%-- Java code --%>
<% Date date = new Date();
   Calendar calendar = Calendar.getInstance(TIME_ZONE).setTime(date);
   SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
   String strdate = sdf.format(calendar.getTime());
%>

<html>
 <body>
 <!-- Does not need to use javascript. All work is done on the server side.-->
 <table>
   <tr>
     <td><%=strdate%></td>
   </tr>
 </table>
 </body>
</html>

I have no idea what your time zone is, but I'm sure you do. Calendar.getInstance() takes an instance of TimeZone as a parameter. That should do it

Take a look:

http://java.sun.com/javase/6/docs/api/java/util/TimeZone.html

http://java.sun.com/javase/6/docs/api/java/util/Calendar.html

Interesting link about JSP

Upvotes: 3

fc.
fc.

Reputation: 181

Initialize the function with date from the server

var d = new Date(<%= new SimpleDateFormat("yyyy, M-1, dd").format(new Date()) %>);

Upvotes: 1

Zack Marrapese
Zack Marrapese

Reputation: 12080

Depending on your server-side language, just use it's date() function which gives the current date/time based on the server clock. Next, format that info and send it to the client as a set value.

For instance, in php:

echo date();

Upvotes: 0

fc.
fc.

Reputation: 181

You've written a javascript function which obviously is dependent on the date set in the client

Upvotes: 0

Related Questions