Created
December 13, 2012 16:13
-
-
Save bswinnerton/4277524 to your computer and use it in GitHub Desktop.
Expiration Date Color Picker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Javascript to change color of expiration date. | |
// Usage: wrap dates in YYYY-MM-DD format with <span class="expiredDate"></span> | |
window.onload = function() { | |
var spans = document.getElementsByTagName('span'); | |
var l = spans.length; | |
for (var i = 0; i < l; i++) { | |
var spanClass = spans[i].getAttribute("class"); | |
if ( spanClass === "expiredDate" ) { | |
var definedDate = spans[i].innerHTML.split("-"); | |
var expiredDate = new Date(definedDate[0], definedDate[1] - 1, definedDate[2]); | |
// Set to 60 day range for grace period | |
var graceDate = new Date(definedDate[0], definedDate[1] - 1, definedDate[2] - 60); | |
var currentDate = new Date(); | |
// Set to red if expired | |
if(currentDate > expiredDate) {spans[i].style.color = '#cc6666';} | |
// Set to yellow if in grace | |
else if(currentDate < expiredDate && currentDate > graceDate) {spans[i].style.color = '#f0c674';} | |
// Set to green for everything else | |
else {spans[i].style.color = '#b5bd68';} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment