Expand my Community achievements bar.

SOLVED

Reading date property using sightly

Avatar

Community Advisor

Hi,

I was trying to read date property using sightly but it wont output anything. Below is the snippet that I used but it will always come as blank:

Date Modified - ${pageProperties.cq:lastModified}

I also tried possibly context options but in vain.

I am trying to avoid Java Helper class route for simply reading one property.

Anyone faced that error before or any solutions to read date property apart from using java helper class?

Thanks,

Runal

1 Accepted Solution

Avatar

Correct answer by
Employee

Here an example how to do date-formatting, done via Sling-models

Sightly: <div data-sly-call="${ dateFormat @ date=currentPage.lastModified, dateFormat='dd/MM/yyyy' }"> </div> <template data-sly-template.dateFormat="${ @ date, dateFormat }"> <div data-sly-use.formatter="${'com.yourproject.DateFormatting' @ date=date,dateFormat=dateFormat}"> Formatted value : ${formatter.formattedValue} </div> </template>
Java: @Model(adaptables=SlingHttpServletRequest.class) public class DateFormatting { @Inject // injected as parameter private Calendar date; @Inject // injected as parameter private String dateFormat; public String formattedValue; @PostConstruct protected void init() { SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); formattedValue = formatter.format(date.getTime()); } }

View solution in original post

9 Replies

Avatar

Level 10

This is all explained in this AEM doc topic:

http://docs.adobe.com/docs/en/aem/6-0/develop/sightly.html

More Sighty examples here:

http://experiencedelivers.adobe.com/cemblog/en/experiencedelivers/2014/01/sightly-preview-part1.html

<ul data-sly-list.child="${currentPage.listChildren}">
    <li>${child.title}</li>
</ul>

 

Avatar

Community Advisor

Hi,

Thanks for the response but the snippet that I pasted in my query is sightly snippet only and it doesnt return anything.

I have gone through the tutorials but none mention how can a date property be accessed from sightly snippet.

- Runal

Avatar

Employee

Can you try this?

${pageProperties['cq:lastModified'].toString}

Avatar

Level 1
No, It doesn't work. Renders Output as:- java.util.GregorianCalendar[time=1578595500000,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="GMT+05:30",offset=19800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2020,MONTH=0,WEEK_OF_YEAR=2,WEEK_OF_MONTH=2,DAY_OF_MONTH=10,DAY_OF_YEAR=10,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=15,SECOND=0,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0]

Avatar

Community Advisor

Hi Fieke,

Thanks it certainly helped. But more interestingly it allowed me to call Date Modified - ${dateobj.getTime.toString}.

My doubt here is, how does sightly allow to call DATE, DATETIME API methods directly i.e dateobj.getTime, dateobj.getTime.getTime, dateobj.getWeekYear etc..

- Runal

Avatar

Employee

Sightly tries to call methods that are available on the object you supplied, just basic java reflection I would say.

Avatar

Correct answer by
Employee

Here an example how to do date-formatting, done via Sling-models

Sightly: <div data-sly-call="${ dateFormat @ date=currentPage.lastModified, dateFormat='dd/MM/yyyy' }"> </div> <template data-sly-template.dateFormat="${ @ date, dateFormat }"> <div data-sly-use.formatter="${'com.yourproject.DateFormatting' @ date=date,dateFormat=dateFormat}"> Formatted value : ${formatter.formattedValue} </div> </template>
Java: @Model(adaptables=SlingHttpServletRequest.class) public class DateFormatting { @Inject // injected as parameter private Calendar date; @Inject // injected as parameter private String dateFormat; public String formattedValue; @PostConstruct protected void init() { SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); formattedValue = formatter.format(date.getTime()); } }

Avatar

Level 1

A little late response, but since I stumbled upon the same problem and came across this post, I am leaving this reply


You have to format it, ie. like so: 

${'yyyy-MM-dd' @ format=myDate}

Check this for more info:
https://github.com/adobe/htl-spec/blob/1.3/SPECIFICATION.md#1222-dates