سؤال

My issue is related to this topic

My requirement is,I need to show the client side date in the UI and I tried many options none of them give results.

The code below shows the correct date which I changed in the machine.But when I deploy the same code in the server which is located in US, the date is not getting changed. It shows the server date and I need to show the user local timezone date wherever the user is accessing the application.
For no reason, I assigned the value of the variable #{sessionScope.TIMEZONE} ="GMT".


Here is my code given in XHTML

<h:outputText
                            value="Current Login Date: #{sessionScope.LOGINTIME}"
                            style="font-weight:normal;white-space:nowrap">
                            <f:convertDateTime type="both" dateStyle="default"
                                timeStyle="default" timeZone="#{sessionScope.TIMEZONE}" />
                        </h:outputText>


Like I said I need to show the local timezone date in the UI.Really appreciate your help.

هل كانت مفيدة؟

المحلول 2

I achieved by this using the below code in XHTML.

<h:outputText id="loggedDt5"
                            style="font-weight:normal;white-space:nowrap" />
                        <SCRIPT type="text/javascript">
                            var now = new Date();
                            document.getElementById('headerform:loggedDt5').innerHTML = 'Current Login Date:'+(now
                                    .getMonth() + 1)
                                    + "/"
                                    + now.getDate()
                                    + "/"
                                    + now.getFullYear();
                        </SCRIPT>

نصائح أخرى

Since it is a client machine from where you need the it is more sensible to use JavaScript to get the required date and display it in the local timezone format.

var d = new Date();
var localTime = d.getTime(); 

var offset = d.getTimezoneOffset();

The documentation for getTimezoneOffset.

Using the offset you can convert to the local timezone. A very good tutorial regarding he same can be accessed at http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top