Display time in user's time zone?

I want to display time in user’s time zone.
But for some reason the server always shows UTC time:

The code I used to display this:

<div class="DateTime">
  <div>Date &<br>Time:</div>
  <div id="VoltageReadingDateValue">$doc.date</div>

I’ve set default timezone to be Singapore in Wiki settings, and In my user settings I just have the default timezone:

Yet the time is displayed for UTC time zone. The edited document in wiki itself is also displayed in UTC time and not in local user time.

I also tried this:

#set ($userDoc = $xwiki.getDocument($xcontext.user))
#set ($userObj = $userDoc.getObject('XWiki.XWikiUsers'))

$datetool.format('default', 'default', $doc.date, 'en', $userObj.get('timezone'))

But instead of a date string, I just get the $datetool.format('default', 'default', $doc.date, 'en', $userObj.get('timezone')) string.

What do I do to fix it?


you should probably use $xwiki.formatDate($doc.date) (see: https://www.xwiki.org/xwiki/bin/view/ScriptingDocumentation/?url=http:%2F%2Fnexus.xwiki.org%2Fnexus%2Fservice%2Flocal%2Frepositories%2Fpublic%2Farchive%2Forg%2Fxwiki%2Fplatform%2Fxwiki-platform-oldcore%2F15.10.8%2Fxwiki-platform-oldcore-15.10.8-javadoc.jar%2F!%2Fcom%2Fxpn%2Fxwiki%2Fapi%2FXWiki.html#formatDate)

This API will use the user timezone information and also the defined format preferences defined in the wiki to display the date.

1 Like

Slightly related too: Loading...

1 Like