Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Show relative dates on plugin admin page

Leirbag Assuab
Contributor
May 22, 2018

Hi Experts!

I'm developing a plugin and I need to show last time its configuration has changed on plugin admin page.

In the servlet invoking the template:

...
context.put("lastUpdate", autoEscalateConfigDao.getRulesLastUpdate()); //java.util.Date
...
response.setContentType(CONTENT_TYPE);
renderer.render(UPLOAD_TEMPLATE, context, response.getWriter());

In velocity template:

#if ($lastUpdate)
<div class="description"><i>Last rules update: $lastUpdate</i></div>
#end

But this is showing something like "Last rules update: 2018-05-22 09:44:58.589"

I would like to show it in relative dates format (X minutes/hours/days ago, yesterday, ...). So I've tried:

$webResourceManager.requireResource("jira.webresources:jquery-livestamp")
...
#if ($lastUpdate)
<div class="description"><i>Last rules update #1: <span data-name="lastUpdate" id="updated-val" data-fieldtype="datetime"><time class="livestamp" datetime="$lastUpdate">$lastUpdate</time></span></i></div>
#end

But still the same. I've tried to surround with $datetimeformatter.format($lastUpdate), but it shows literally "$datetimeformatter.format($lastUpdate)".

I've been looking a lot of similar posts but I can't found a complete example.

I would like this date was shown in relative date format and changing when time passes, as date fields do in issue view screen.

Could you please give me some ligth about how to get this?

Thanks in advance!

________

Leirbag

3 answers

1 accepted

1 vote
Answer accepted
Aleksandr Zuevich
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 22, 2018

Hi,

you're almost done. I propose two things:

1) change lastUpdate type to Long and set the value autoEscalateConfigDao.getRulesLastUpdate().getTime();

2) add AJS.$("time.livestamp").livestamp() invocation to initialize livestamp.

Leirbag Assuab
Contributor
May 23, 2018

Hi @Aleksandr Zuevich,

Bingo! Your proposal was just what was missing in my code. I've added these two things and now it's working exactly as I want.

Thanks!!

1 vote
Leirbag Assuab
Contributor
May 23, 2018

For beginners like me needing more detailed code. This is a summary with key points thanks to invaluable help by Aleksandr.
Servlet:

package your.package;
...
@Named("YourServlet")
public class YourServlet extends HttpServlet
{
...
@ComponentImport
private final DateTimeFormatter dateTimeFormatter;
...
@Inject
public YourServlet(DateTimeFormatter dateTimeFormatter, ...)
{
this.dateTimeFormatter = dateTimeFormatter;
...
}
@Override
protected void doGet/doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
...
Map<String, Object> context = Maps.newHashMap();
if (autoEscalateConfigDao.getRulesLastUpdate() != null) {
context.put("lastUpdate", autoEscalateConfigDao.getRulesLastUpdate().getTime());
DateTimeFormatter dtf = dateTimeFormatter.forLoggedInUser();
context.put("lastUpdateHTML", dtf.format(autoEscalateConfigDao.getRulesLastUpdate()));
}
...
response.setContentType(CONTENT_TYPE);
renderer.render(UPLOAD_TEMPLATE, context, response.getWriter());
}

Velocity template:

...
<head>
...
$webResourceManager.requireResource("jira.webresources:jquery-livestamp")
...
</head>
...
#if ($lastUpdate)
<div class="description"><i>Last rules update: <time class="livestamp" datetime="$lastUpdate" title="$lastUpdateHTML">$lastUpdate</time></i></div>
<script type = "text/javascript">
AJS.$("time.livestamp").livestamp()
</script>
#else
<div class="description"><i>Last rules update: Never.</i></div>
#end
...

After page it has been rendered and js executed it shows something like: "Last rules update: 30 minutes ago". Time refreshes automatically and it's updated until user navigate out. When user hovers over the time tag then exact moment is shown.

Hope it would be helpful for someone.

0 votes
Leirbag Assuab
Contributor
May 23, 2018

<repeated answer due to technical problems>

Suggest an answer

Log in or Sign up to answer