Archive

Posts Tagged ‘date’

SharePoint 2013: Converting GMT/UTC time to local time in XSLT for rendering RSS feed

September 28, 2017 Leave a comment

Hi Friends,

Recently I came across the requirement where the date coming from RSS feed which is in GMT format needed to be converted to local time and display in required format.

As RSSAggregatorWebPart webparts executes on server side and renders the output html in browser, we cannot utilize the rss date to convert to local date.

I tried multiple solution with default XSLT for calling JavaScript function, like CDATA, xsl:comment and try adding script tag but none worked for me.

Finally after struggling I found a way to achieve desired result using onerror handler with img tag.

Solution as fallows –

XSLT changes

Add following section in XSLT, which actually uses the img tag to call the JavaScript function using on error event.

Rss1

JavaScript changes

Following function we needed to add a separate JS file which will be referred in the master page or add in master page itself and make sure it that JS method gets loaded before the method call in XSLT.

function ConverDateToLocal(pDate, sender) {
    try {
        var today = new Date(pDate);
        var dd = today.getDate();
        var mm = today.getMonth() + 1; //January is 0!
        var yyyy = today.getFullYear();
        var hh = today.getHours();
        var mmm = today.getMinutes();
        /*if(dd<10) {
            dd='0'+dd
        } 

        if(mm<10) {
            mm='0'+mm
        } */
        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;
			                // After testing folling code should be execuated
			                //datediv.innerHTML = restStr;
                        }
                    }
                }
            }
        }
    }
    catch(err) {}
}

After applying the XSLT we can see the output like following where first date is GST date and next date is converted local date.

RSS3

Happy coding.

Advertisements

Angular JS: date formatting in ng-repeat

Hi Friends,

I was binding some data using angular JS with ng-repeat, in my case there are couple of columns which are dates. From the data source it is coming in different format something like full date as 2017-04-21T09:01:00.000Z

Now I wanted to format it without changing my data source array, one option would be to create a JavaScript Date object and then convert in required format and then update the value in array.

Angular have some easy way to achieve date formatting with ng-repeat.

<div class="dateCreated">{{myobj.CreatedDate | date:'M/d/yy h:mm a'}}</div>

Or we can use fixed formats provided by Angular like ‘short’ or ‘mediumDate’, example

<div class="dateCreated">{{myobj.CreatedDate | date:'short'}}/div>

There are some other formats also available, please refer Angular Date formatting API documentation at https://docs.angularjs.org/api/ng/filter/date

Happy learning!