0
votes

I'm hoping that this should just be a simple issue for someone who regularly writes User Macros for confluence.

I'd simply like to just display the week of year on the page. I have created the user macro with the usual metadata and included the following to just show the current date for now:

## @noparams
<div>$content.currentDate</div>

The version of confluence is 5.9.8 and I have searched the documentation to try and find any methods for the 'currentDate' property, but it isn't documented that the currentDate field even exists on the ContentEntityObject ($content). I have tried to use '$content.currentDate.get(3)' as I believe in Java this returns the week of the year but then Confluence just renders the entire block as plain text.

2

2 Answers

0
votes

Accessing Java objects can get tricky in velocity.

In theory, if you could access SimpleDateFormat, you would do something like this...

@noparams
#set( $dow = new SimpleDateFormat("EEEE").format($content.currentDate()) )
<div>$dow</div>

But unfortunately, SimpleDateFormat is not easily accessible from velocity.

If client-side rendering is acceptable, then it's much easier - simply roll your own JavaScript:

@noparams
<div class="dow"></div>
<script>
  (function() {
      var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
      Date.prototype.getDayName = function() {
          return days[ this.getDay() ];
      };
  })();

  AJS.toInit(function ($) {
    $('.dow').text( (new Date()).getDayName() );
  });
</script>
0
votes

Building on what @dvdsmpsn has provided my macro currently looks like this:

## @noparams

<div id='weeknumber'></div>

<script>
function getWeekNumber(d) {
    d.setHours(0,0,0,0);
    d.setDate(d.getDate() + 4 - (d.getDay()||7));
    var yearStart = new Date(d.getFullYear(),0,1);
    var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
    return weekNo;
}

AJS.toInit(function ($) {
    $('#weeknumber').text(getWeekNumber(new Date()));
  });
</script>

There are a few nuisances:

  1. Moment is already loaded in confluence but I can't seem to access it from the Macro environment
  2. The HTML Macro environment had to be enabled to allow the use of Javascript.
  3. The Macro had to be used at the base of the confluence page as its use seemed to overwrite all following text that was written in the editor when viewed in the read-only format (after the jQuery method was run)