I'm creating a traffic light system that dynamically compares today to the due date, and populates a calculated column accordingly. I was able to code it, but the user changed requirements. Instead of the yellow traffic light showing if the item is due today, they want it to show if the item is due within the next 5 days.
I'd like to change it from "else if (fieldDate == today) " to five days before today. I can't figure out the proper way to create a today - 5 object in JavaScript. Help!
<script type="text/javascript">
//<!-- This script searches for calculated fields that are "marked" vith "Due:" and -->
//<!-- Create a calculated field in the list with the following formula: -->
//<!-- =IF(DueDate="","N/A","Due: "&MONTH(DueDate)&"/"&DAY(DueDate)&"/"&YEAR(DueDate)) -->
//<!-- The data type returned from this formula is: Date and Time -->
// call script
findDatefields();
function findDatefields() {
var d = new Date();
var today = new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
var arr = document.getElementsByTagName('td');
for (var i = 0; i < arr.length; i++) {
// Check if it is "our field"
if ((arr[i].className == "ms-vb2") && (arr[i].innerHTML.indexOf("Due:") == 0)) {
var sepDate = arr[i].innerHTML.substring(5).split("/", 3);
var m = sepDate[0];
var d = sepDate[1];
var y = sepDate[2];
// build the datestring
var fieldDate = new Date(y, m - 1, d, 00, 00, 00).getTime();
if (fieldDate > today) {
arr[i].innerHTML = "<IMG src='_layouts/images/KPIDefault-0.gif' Title='On track' />";
}
else if (fieldDate == today) {
arr[i].innerHTML = "<IMG src='_layouts/images/KPIDefault-1.gif' Title='Due today' />";
}
else {
arr[i].innerHTML = "<IMG src='_layouts/images/KPIDefault-2.gif' Title='Overdue' />";
}
}
}
}
// For it to work in collapsed views
function ExpGroupRenderData(htmlToRender, groupName, isLoaded) {
var tbody = document.getElementById("tbod" + groupName + "_");
var wrapDiv = document.createElement("DIV");
wrapDiv.innerHTML = "<TABLE><TBODY id=\"tbod" + groupName + "_\" isLoaded=\"" + isLoaded + "\">" + htmlToRender + "</TBODY></TABLE>";
tbody.parentNode.replaceChild(wrapDiv.firstChild.firstChild, tbody);
findDatefields();
}
</script>