0

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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
sebpinski
  • 101
  • 1
  • 4

2 Answers2

0

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>
dvdsmpsn
  • 2,839
  • 1
  • 26
  • 40
  • Does Confluence include the velocity tools, e.g., DateTool? – Dave Newton May 16 '17 at 09:17
  • Very useful answer. Seems a shame that within the macro environment I don't appear to be able to access moment.js. I've used your answer combined with the one [here](http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php) to get what I need. There were a few problems though: 1. I had to enable the HTTP as described [here](https://confluence.atlassian.com/confkb/how-to-use-javascript-in-confluence-313458839.html) 2. I had to put the macro at the base of the page as the writing of the text appeared to overwrite all following text in the page once called – sebpinski May 16 '17 at 10:32
  • Oops, just noticed that you said **week of year** *not* **day of week**. – dvdsmpsn May 16 '17 at 12:14
0

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)
sebpinski
  • 101
  • 1
  • 4