0

I am trying to compare dates and apply a class based on the result.

<div class="" id="date" title = <%... %> />

The title is the date and time returned from a query (IE. Mar 23, 2016 05:00 AM). I would like to check if the DATE is less than today (ie Mar 23, 2016). I am sure this will require a function, but was hoping there may be a shortcut I am missing. Any help is appreciated. Not very proficient in jQuery yet.

EDIT:

Maybe I am going about this wrong. As stated this is a resultset from a query that is in a nested repeater. If the date returned is less than the current date (time is of no consequence) then I want to add a class to the given label. this will be repeated for each returned result. Should I look at doing this via Java script post databinding or code-behind (C#)? Just getting back into programming after several years of DBA work. Thanks again in advance

Mike C
  • 63
  • 1
  • 11

2 Answers2

0

You can use Date.parse() to compare those string dates to see if one is greater than the other. See This StackOverflow post about the same topic and This MDN article on Date for more information.

Community
  • 1
  • 1
tommymcdonald
  • 1,528
  • 2
  • 10
  • 12
0

Try this

function isPrevious(date, compareWith){
    var check_date = new Date(date);
    var today_date = new Date(compareWith);

    return check_date.getTime() < today_date.getTime();
}

Test

console.log(isPrevious('Mar 22, 2016 05:00 AM', 'Mar 23, 2016 05:00 AM'));
console.log(isPrevious('Mar 22, 2016 05:00 PM', 'Mar 22, 2016 00:01 PM'));

Fiddle: https://jsfiddle.net/obcam1vc/

Veniamin
  • 459
  • 2
  • 9