on ‎2017 Dec 21 6:27 AM
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
Request clarification before answering.
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;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can use google api to get timezone.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.