JS Conditional Formatting with Date Field using SharePoint

495 Views Asked by At

I am new to JS so please be patient with me. I am making a JSLink file to add conditional formatting to my SharePoint page.

I have a date field in my SharePoint page (viewed in Chrome Debugger mode):

"Calibration_x0020_Due2": "3\u002f31\u002f2020",

I would like to make some code check if the date is after 30 days prior to today's date (to make the background red). It will be green if the date is greater than 30 days to prior today's date.

I am modifying a function I have that applied conditional formatting depending on what word was displayed in another field. I am not sure how I go about checking the date.

(function () {

var statusFieldCtx = {};
statusFieldCtx.Templates = {};
statusFieldCtx.Templates.Fields = {
    "Calibration_x0020_Due2": {  //The internal name of that column.
                "View": StatusFieldViewTemplate
        }
};

SPClientTemplates.TemplateManager.RegisterTemplateOverrides(statusFieldCtx);

    function StatusFieldViewTemplate(ctx) { //Function to change the status styling

    var statusValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name]; //To get the current item's status value.
        if (statusValue < "NOT SURE WHAT TO PUT HERE") {
                return "<div style='background-color:red;color:white'>" + statusValue + "</div>";
        }
        if (statusValue >= "NOT SURE WHAT TO PUT HERE") {
            return "<div style='background-color:green;color:white'>" + statusValue + "</div>";
        }
        else{
                return statusValue;
        }
    }
})();

Clearly the math is obvious. I just cannot figure out how to pull the date from the string? I have tried statusValue.getMonth() and such, but it says .getMonth is not a function among other errors.

Any thoughts?

0

There are 0 best solutions below