kardanov
kardanov

Reputation: 675

Displaying dateTime values with client time zone in JSF

I have following code:

<h:outputText value="#{myDataBean.date}">
    <f:convertDateTime type="both" timeStyle="short" dateStyle="short" />
</h:outputText>

How can I display this dateTime element with client time zone in JSF (Richfaces)? I guess I should use timeZone attribute, but what should I specify as its value?

Any help will be appreciated.

Upvotes: 4

Views: 5242

Answers (1)

Mark
Mark

Reputation: 17132

The problem is you won't know what the clients timezone is to set the timeZone attribute, the only way I can think of to do this is to convert the date in Javascript.

The <f:convertDateTime> converter defaults to UTC time.

<h:body onload="setTimezoneDates();" >
    <h:form id="myform">
      <h:outputText value="#{indexBean.today}" id="mydate">
        <f:convertDateTime type="both" timeStyle="short" dateStyle="medium" />
      </h:outputText>
    </h:form>
</h:body>

And then use Javascript to convert your UTC date/time to the clients local date/time and replace it on the page.

function setTimezoneDates() {
    var strDate = document.getElementById("myform:mydate").innerHTML + " UTC";
    var d = new Date(strDate);                                      
    document.getElementById("myform:mydate").innerHTML = d.toLocaleString();
}

The output date still needs to be formated and there is probably a better way to do this in Javascript. Also note I had to set the <f:convertDateTime> dateStyle to medium for this to work in IE.

Upvotes: 4

Related Questions