Snippet O’ the Week: jQuery, Highlight Element Based on Current Date
This weeks snippet o the week is brought to you by the letter ‘j’ and the word ‘Query’. This snippet is actually really cool and can solve a lot of usability problems. I found this snippet over at the great jQuery4u.com and it’s a slick litte function that will highlight an element with the current date and time – pretty slick no?
Here’s the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/**
* $.dateTimeHighlightNow()
* Author: Sam Deering
* Adds/removes a selected class on elements based on the current date and time.
* usage: $('.program p').dateTimeHighlightNow();
*/
jQuery.fn.dateTimeHighlightNow = function()
{
return this.each(function()
{
var datetimestamp = Math.round(new Date().getTime() / 1000)
elem = $(this),
start = elem.attr('start'),
finish= elem.attr('finish');
log('datetimestamp = '+datetimestamp);
if (start < datetimestamp && finish > datetimestamp)
{
elem.addClass('selected');
log(elem);
}
else
{
elem.removeClass('selected');
}
});
};
|
Here is a screenshot from jquery4u.com:
For the full HTML and implementation example, hit the tutorial here: http://www.jquery4u.com/jquery-functions/jquery-highlight-element-based-current-date-time/. Big thanks to jquery4u.com, I did use this on a project last week and it works splendidly. I did modify the HTML implementation to work with my li’s, rather than the paragraph examples.

Hi Patrick. Is there any way you could assist me with applying this code to a table I have where I need a row highlighted based on the current date? Thanks very much.
Sorry for the delayed response. I would try using getDate() somehow instead of getTime(). Take a look at this link and see if it will work for ya. http://stackoverflow.com/questions/8398897/how-to-get-current-date-in-jquery