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>";
}
}
String.formathttp://sharepoint.stackexchange.com/questions/160806/changing-date-format-using-javascript/160808#160808 – Danny '365CSI' Engelman Mar 04 '16 at 18:02