SharePoint 2013 - Converting GMT/UTC Time To Local Time In XSLT For Rendering RSS Feed Results

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.

SharePoint

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.

  1. function ConverDateToLocal(pDate, sender) {  
  2.     try {  
  3.         var today = new Date(pDate);  
  4.         var dd = today.getDate();  
  5.         var mm = today.getMonth() + 1; //January is 0!  
  6.         var yyyy = today.getFullYear();  
  7.         var hh = today.getHours();  
  8.         var mmm = today.getMinutes();  
  9.         /*if(dd<10) { 
  10.  
  11.         dd='0'+dd 
  12.  
  13.         } 
  14.  
  15.         if(mm<10) { 
  16.  
  17.         mm='0'+mm 
  18.  
  19.         } */  
  20.         var restStr = dd + '.' + mm + '.' + yyyy + " " + hh + ":" + mmm;  
  21.         if (sender != null) {  
  22.             var senderid = sender.id;  
  23.             if (senderid) {  
  24.                 var senderidsplit = senderid.split('_');  
  25.                 if (senderidsplit.length > 0) {  
  26.                     var datedivid = "datediv_" + senderidsplit[1];  
  27.                     if (datedivid) {  
  28.                         var datediv = document.getElementById(datedivid);  
  29.                         if (datediv != null) {  
  30.                             datediv.innerHTML += " :: " + "New ::" + restStr;  
  31.                             // After testing folling code should be execuated  
  32.                             //datediv.innerHTML = restStr;  
  33.                         }  
  34.                     }  
  35.                 }  
  36.             }  
  37.         }  
  38.     } // try ends  
  39.     catch (err) {}  
  40. // function ends  

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.

SharePoint

Happy learning.