views:

489

answers:

2

In Javascript, I have this function to display the current date on our page header:

<SCRIPT language="Javascript">
    var today = new Date();
    document.write(today.toLocaleDateString());
</SCRIPT>

I would like to do this via JSTL, but I'm not sure if it's possible. So far, I have this fragment of code:

<jsp:useBean id="date" class="java.util.Date" />
<fmt:formatDate value="${date}" type="date" pattern="EEEE, MMMM dd, yyyy"/>

Because the date is now being created on the server, it may not represent the client's date. I believe that I can set the timeZone attribute of the formatDate function, but I'm unsure how to grab the client's timezone. Can somebody offer a suggestion?

Thanks!

+2  A: 

All the information the server has is in the HTTP request which doesn't contain any info regarding time or timezone.

So you have two options:

  • Using a cookie you can store the timezone and then retrieve it in the server-side. The problem here is that you have to wait for the second request to use the timezone value.

  • Using AJAX, with javascript and a XMLHttpRequest object you can push 'new Date().getTimezoneOffset()' value to the server, store it in a session var

Maybe any of the options are too much if you just want to display time zone

victor hugo
That's certainly a creative idea, but more complicated than the SCRIPT I currently have...
T Reddy
Yes, I poster this answer just if knowing the timezone is critical. All the information the server has is in the HTTP request which doesn't contain any info regarding time or timezone, so I guess it is impossible to know it if not using AJAX or a cookie
victor hugo
+1  A: 

The only way I can see of doing this without asking the user for some information is to format the date with javascript. Not exactly the prettiest solution, I might add

<script ...>document.write(<javascript to format the date>)</script>

Alternately, you might consider displaying the time zone in the formatted date. This way, the user at least knows what time zone you're using.

David