Recently, I came across a requirement where the date-time coming from an RSS feed was in GMT format. We needed to convert that to local time and displayed in the required format.
As RSSAggregatorWebPart executes on the server side and renders the output in HTML in the browser, we cannot utilize the RSS date to convert to local date.
I tried multiple solutions with default XSLT for calling JavaScript function, like CDATA, xsl:comment, and try adding <script> tag but none worked for me.
Finally, after struggling a lot, I found a way to achieve the desired result using onerror handler with <img> tag.
Solution is as follows,
XSLT changes
Add the following section in XSLT, which actually uses the img tag to call the JavaScript function using on error event.
JavaScript changes
In the following function, we needed to add a separate JS file which will be referred to in the master page or added in the master page itself. Make sure that JS method gets loaded before the method is called in XSLT.
- function ConverDateToLocal(pDate, sender) {
- try {
- var today = new Date(pDate);
- var dd = today.getDate();
- var mm = today.getMonth() + 1;
- var yyyy = today.getFullYear();
- var hh = today.getHours();
- var mmm = today.getMinutes();
-
-
-
-
-
-
-
-
-
-
-
- var restStr = dd + '.' + mm + '.' + yyyy + " " + hh + ":" + mmm;
- if (sender != null) {
- var senderid = sender.id;
- if (senderid) {
- var senderidsplit = senderid.split('_');
- if (senderidsplit.length > 0) {
- var datedivid = "datediv_" + senderidsplit[1];
- if (datedivid) {
- var datediv = document.getElementById(datedivid);
- if (datediv != null) {
- datediv.innerHTML += " :: " + "New ::" + restStr;
-
-
- }
- }
- }
- }
- }
- }
- catch (err) {}
- }
After applying the XSLT, we can see the output like following where the first date is GST date and the next date is converted into the local date.
Happy learning.