views:

640

answers:

4

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..

A: 

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

fc
yes, i have written a javascript function which is dependent on date set in the client,, but i want that to be independent of client.. if client changes his date on the system then that would not reflect it on webpage,, is there any logic to solve this...
AGeek
A: 

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();
Zack
i have tried this,, it does not take the time of server, rather it takes the time of client machine.. and i have tested this by putting a code on a server, and accessing it to my computer (with date and time changed). but it reflects the date of the client rather server.. so i need to know some website which offer this date etc..
AGeek
+1  A: 

Initialize the function with date from the server

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

fc
giving error... not working!!
AGeek
What's the error?
R. Bemrose
Have you imported `java.text.SimpleDateFormat` and `java.util.Date`
fc
+3  A: 

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

Tom
i was not able to run your example accurately, but i can imagine ur program will give me the time of the server on the webpage, if you can write the code complete,, using inside a table will do...
AGeek
hi sir,, your code worked.. but one problem now i am facing is that the server is based at US, and i am from diff. country so time not matching,, not thought of this before,, so can i code something or get the time from some other website everytime the page loads, and comes up with my country's time..
AGeek
You should take a look at the java.util.Calendar class, particularly at the method setTimeZone()
Tom
plz specify some example of TIME_ZONE.. lets say, how to write the timezone for GMT+3.5 in the programm..
AGeek
From TimeZone Class, see link above (TimeZone.html)Hours must be between 0 to 23 and Minutes must be between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten hours and ten minutes ahead of GMT, respectively.
Tom
Thnx a lot sir,, i solved my doubt.. All because of u, i achieved it.. thnx..
AGeek