1

I am using the above method to format my date, but for some reason it is formatting the date using the English US format: MM/dd/YYYY even though the locale for my site is English United Kingdom.

I tried the following variation: toLocaleDateString('en-GB') but it is still not working either. Any ideas?

Here is a copy of my code:

(function () {
    var statusFieldCtx = {};

    statusFieldCtx.Templates = {};
    statusFieldCtx.Templates.Fields = {
        "MaxEndDate": {
            "View": ColorCodeDueDate
            }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(statusFieldCtx);
})();

function ColorCodeDueDate(ctx) {
    var dueDate = new Date(ctx.CurrentItem.MaxEndDate);

    var now = new Date();
    var then = new Date();
    then.setDate(now.getDate() + 180);

    // if there's no due date don't render anything
    if (dueDate == 'undefined' || !dueDate) {
        return '';
    }
    else if (dueDate >=now && dueDate <= then) { // we are within 180 days
        return "<div style='background-color:red;color:white'>" + dueDate.toLocaleDateString('en-GB') + "</div>";
    }
    else {  // we are outside of 180 days
        return "<div>" + dueDate.toLocaleDateString('en-GB') + "</div>";
    }
}
  • if you open the developer console (say on chrome), and you put this in console.log(new Date().toLocaleDateString('en-GB')); what do you get? – Chunkyfeather Mar 4 '16 at 10:02
  • I get ‎04‎/‎03‎/‎2016 which is correct. so why is this not working when I apply that code as JSLink to a webpart? this code is supposed to manipulate a column called "MaxEndDate" – Luis Carvalho Mar 4 '16 at 11:48
  • Thats odd.Do your dates show in dd/MM/yyyy format within SharePoint when viewing lastmodified or something like that? I know it may seem weird, but I have seen this issue before, I had to change the globalization setting within iis, but this wasnt for SharePoint. Why not try this library momentjs.com? – Chunkyfeather Mar 4 '16 at 12:34
  • @Chunkyfeather . Yes my dates look fine in other columns. As my knowledge in javascript is very basic, could you please tell me in the above code where would I insert that modification? thks – Luis Carvalho Mar 4 '16 at 14:21
  • See what you can do with String.format sharepoint.stackexchange.com/questions/160806/… – Danny '365CSI' Engelman Mar 4 '16 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.