Friday, May 6, 2011

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

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

    AGeek : 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...
  • 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();
    AGeek : 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..
  • Initialize the function with date from the server

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

    AGeek : giving error... not working!!
    R. Bemrose : What's the error?
    fc : Have you imported `java.text.SimpleDateFormat` and `java.util.Date`
  • 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-->

    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 
    // or a String

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

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


    <% //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());
     <!-- Does not need to use javascript. All work is done on the server side.-->

    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:

    Interesting link about JSP

    AGeek : 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..
    Tom : You should take a look at the java.util.Calendar class, particularly at the method setTimeZone()
    AGeek : plz specify some example of TIME_ZONE.. lets say, how to write the timezone for GMT+3.5 in the programm..
    Tom : 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.
    AGeek : Thnx a lot sir,, i solved my doubt.. All because of u, i achieved it.. thnx..


Post a Comment