cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Show User's timezone instead of Server timezone

Former Member
0 Likes
2,971

Hello Experts,

We have a requirement, where our shop will be accessed from different timezones, so we want to display time as per user's location timezone instead of server's timezone on order confirmation page. One more thing to note is that, we do not want to store user specific timezone time in DB, we just want show it wherever applicable.

Edit: We could identify user's timezone through http request and then convert time to that timezone, however we need to know if Hybris has OOB solution to fulfil this requirement.

Regards, Rupesh

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Likes

To get user's timezone you need to capture the Localtimezone from browser via JS. Hybris will return same timezone always as set in the application server (suppose UTC). You need to then convert that to LocalTimeZone using JS and display in UI -

Hybris OOTB comes with below TimeZone related JS files - (used in backoffice for Date attributes)

 <script type="text/javascript" src="${commonResourcePath}/js/moment.js"></script>
 <script type="text/javascript" src="${commonResourcePath}/js/moment-timezone-with-data-2010-2020.js"></script>
 <script type="text/javascript" src="${commonResourcePath}/js/jstz.min.js"></script>

Then you can call the below function to convert application timezone (UTC) to Local Timezone -

 convertUTCtoLocalTime: function(vDate, format) {
         moment.suppressDeprecationWarnings = true;
         if(vDate) {
             var localTz;
             if(typeof(Intl) !== "undefined") {
                 localTz = Intl.DateTimeFormat().resolvedOptions().timeZone; //get local TimeZone
             }
             if(!localTz && typeof(jstz) !== "undefined") {
                 localTz = jstz.determine().name(); //if not available, try jsTZ API (for IE11 and old browsers)
             }
             if(!localTz) {
                 localTz = "America/New_York"; //if still not available, set EST as default local TimeZone
             }
             vDate = moment.tz(vDate, localTz).format(format); //format date based on local TimeZone
         }
         return vDate;
     }

former_member387866
Active Contributor
0 Likes

Hi Rupesh,

The JsTz library can help your storefront.
This StackOverFlow Q&A "get client time zone from browser" has lots of good advice.


Out of the Box:

You can use userService.getCurrentUser().CURRENTTIME, it might need to be formatted. You can use the de.hybris.platform.util.Utilities class for date formatting.

The Jalo Session Session Context has the timezone too.
JaloSession.getCurrentSession().getSessionContext().getTimeZone(), or even getTimeOffset() if needed.


I hope this points you in the right direction,
Luke

Former Member
0 Likes

Hi Luke,

Thanks for the quick reply, actually I was looking for any possible OOB way to fulfil this requirement.

Thanks,

former_member387866
Active Contributor
0 Likes

Hi Rupesh, I've updated my answer with some OOTB methods that you can use.

Former Member
0 Likes
Former Member
0 Likes

Hi Kiran,

Thank you for quick reply, actually we are able to identify user's timezone through http request and also we can then use it to convert time to that specific time zone. However, we wanted to know if Hybris has any OOB provision for achieving the same.