0

I am working with a SharePoint list that has a date & time type column called Change Window Start (internal name: Start_x0020_Time). I am using below code to highlight the item row and font color if change window start date is today but it is not working. Can someone help me correcting the code.

Again the data type is Date and time for change window start column.

<script type="text/javascript">
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() { 
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
        OnPostRender: function (ctx) {
            // get today's date
            var today = new Date();
            // zero out the time portion so we will only compare days
            today.setHours(0,0,0,0);
            var rows = ctx.ListData.Row;
            for (var i = 0; i < rows.length; i++) {
                // get the date set in your date YourDateField
                var itemDate = new Date(rows[i]['Start_x0020_Time']);
                // zero out the time portion so we only compare days
                itemDate.setHours(0,0,0,0);
                var rowId = GenerateIIDForListItem(ctx, rows[i]);
                var row = document.getElementById(rowId);
                if (row!=null&&itemDate === today) {                  
                    row.style.backgroundColor = 'red';  

                }
            }
        }
    });
});
</script>

I tried using both display name and internal name for Change window start but neither of that works.FYI, I am working with SPOnline.

  • This can be done using json formatting in modern experience. is there any other reason for using classic experience? – Ganesh Sanap Mar 13 at 13:56
  • 1
    I have a calculated column based on change window start and change window end date & time column. When adding JSON code for a date column, it seems to affect the calculated column, odd but true. – mdevm Mar 13 at 14:41
1

First thing, you need to use internal name while getting the date field.

So, change

var itemDate = new Date(rows[i]['Change Window Start']);

to

var itemDate = new Date(rows[i]['Start_x0020_Time']);

And change condition in your IF statement as given below:

if (row!=null && itemDate == today)

Update:

Or You can also build the logic something like:

itemDate <= today.setHours(23,59,59) && itemDate >= today.setHours(0,0,0) 
  • You can also build the logic something like, itemDate < today.setHours(23,59,59) && itemDate > today.setHours(0,0,0) . And if you want to use json formatting and modern experience then check my answer given here. – Ganesh Sanap Mar 13 at 14:15
  • I updated the script in the question info, still no luck with it. – mdevm Mar 13 at 14:35
  • Are you getting any error in console? Or wrong rows get colored? Or nothing happens at all? – Ganesh Sanap Mar 13 at 14:59
  • Upon running the script on console, i see following message: Uncaught SyntaxError: Unexpected token <. However I don't see any additional '<' being used in the script. – mdevm Mar 13 at 15:10
  • One thing I am noticing is, the same script works for conditions: item date<=today or item date >= today but when it comes to equal to it does not work. So item date===today, item date==today or item date = today does not work it seems. thoughts? – mdevm Mar 13 at 15:16
1

Multiple errors found at a glance:

new Date(rows[i]['Change Window Start']); should use the internal column name.

if (row!=null&&itemDate = today) here you should use === like this: if (row !== null && itemDate === today).

Best thing to do is to use the developer tools in the browser to debug the code and see where it goes wrong

  • Thanks Robert, used internal name and made the if condition change as well but still no luck, thoughts? – mdevm Mar 13 at 14:36
  • Robert it seems that comparison item date == today, item date===today and itemdate =today is not working. Althougth >= and <= works fine. Will euqal to comparison be more complicated since the column is of type date and time?Any thoughts? – mdevm Mar 13 at 15:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.