Our current approach involves the following steps:
- Create a
Date
object for today's date.
- Select all
td
elements that contain a date, identified as the second td
.
- Retrieve the expiration date and create a new
Date
object using that string value.
- Generate a
Date
object representing a month from now by adding 1 month to the current date.
- Determine if the expiration date falls before today or within the next month and assign the corresponding highlight class.
- If a highlight class is specified, apply it to the element.
This solution implements two types of highlights:
- For expiration dates within a month ahead.
- For expired dates.
var today = new Date(),
toISO8601 = function ( dateStr, offset ) {
return dateStr.replace( ' ', 'T' ) + ( offset ? offset : 'Z' );
};
$( 'td:nth-child( 2 )' ).each( function( i, el ) {
var $el = $( el ),
expireStr = toISO8601( $.trim( $el.text() ), '-05:00' ),
expires = new Date( expireStr ),
inAMonth = new Date( today.getTime() ),
highlight;
inAMonth.setMonth( inAMonth.getMonth() + 1 );
highlight = expires < today ? 'has-expired' :
expires < inAMonth ? 'about-to-expire' :
false;
if ( highlight ) {
$el
.parent()
.addClass( highlight );
}
} );
.has-expired {
background-color: indianred;
}
.about-to-expire {
background-color: pink;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>Domain</th>
<th>Renewal Date</th>
</tr>
<tr>
<td>mydomain.com</td>
<td>2017-04-14 17:21:00</td>
</tr>
<tr>
<td>mydomain.net</td>
<td>2017-08-14 17:21:00</td>
</tr>
<tr>
<td>mydomain.net</td>
<td>2017-03-10 17:21:00</td>
</tr>
<tr>
<td>mydomain.net</td>
<td>2017-04-15 17:21:00</td>
</tr>
<tr>
<td>mydomain.net</td>
<td>2018-06-28 17:21:00</td>
</tr>
<tr>
<td>mydomain.net</td>
<td>2017-04-17 10:21:00</td>
</tr>
</table>
You have the option to utilize toISOString()
, such as expires.toISOString()
, in place of the custom toISO8601()
method, although toISOString()
displays time in UTC while our function allows setting your own timezone offset.
Dealing with time can be complex, and depending on requirements, a library like moment.js
may be necessary.