Tim Reddy
Tim Reddy

Reputation: 4438

Is it possible to use JSTL to display a date in the client's timezone?

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!

Upvotes: 3

Views: 2724

Answers (2)

David
David

Reputation: 1251

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.

Upvotes: 1

victor hugo
victor hugo

Reputation: 35848

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

Upvotes: 2

Related Questions