开发者

Find HTML Date then Compare to Current Dat

I need to alert patrons when the library card is about to, or has expired. The page they see always displays their expiration date in this format:

EXP DATE:10-08-2011

or

EXP DATE:mm-dd-yyyy

First I need to grab that date and compare it to today's date. I then need to alert the patron in some way that their card will expire in less than 30 days, or let them know that their card has already expired.

How do I grab that date from the string? How do I compare that to today's date? How do I make the comparison differentiating between less than 30 days in the future and in the past? How create an alert for each scenario?

I'm not looking for a complete solution, just to be pointed in the right direction.

Any help is greatly appreciated,

kern

This is what I ended up doing for the date split and comparison. As a complete novice, it was easier to conceptualize. However, please feel free to give advice and cleaner ways to perform this... Now I just need to figure out how to "grab" the date from the html.

Thanks for the help!

kern

//below split date on hyphens

var s = '10-3-2011';
var fields = s.split(/-/);
var month = fields[0];
var day = fields[1];
var year = fields[2];

//below replace hyphens with slashes
var slashDate = (month + "/" + day + "/" + year)

// below convert string to date
var patexpDate = new Date(slashDate);

//below get todays date
var todaysDate = new Date();

//below patron ex开发者_运维知识库piration date minus todays date
var diffDates = (patexpDate - todaysDate)


if (diffDates >= 0 && diffDates <= 2592000000) {
alert('Your Library card expires in less than 30 days.');

}

else if (diffDates < 0) {
alert("Your Library card has expired.");

}

else alert('Card NOT expired.');


Basically, you just need to grab the date components from your element's HTML.

 var dateString = someElement.innerHTML;

Then, split that by your separator and pull each component:

 var components = dateString.split('-');

 var day = components[1];
 var month = components[0];
 var year = components[2];

Then, make a new Date object with that data:

 var expirationDate = new Date(day, (month - 1), year); 
 // Remember, JavaScript uses 0-11 to represent months!

Then, get the current date, and then perform some simple math to determine the date range:

 var now = new Date();

 if(now.getTime() >= expirationDate.getTime())
 {
     // Exp Date is in the past
 }
 else
 {
     // Exp date in the future
 }

That should get your started.


There's a simpler way to get your expiration date. Javascript allows you to construct dates by passing in a string variable. So you can do something like this.

var exDateString = dateElem.innerHtml, // comes out to your date as a string 10-08-2011
    exDateObj = new Date(exDateString), // gives a data object with your expiration date
    today = new Date();

now to start with, you can do a simple check to see if it is expired with normal operators.

if (exDateObj >== today) { // if expiration date is greater than or equal to today
    alert('Your card is expired');
}

not sure the beset way to get the number of days until it will expire but you can just write through if/else statements to go through the difference scenarios.

forgot to link this, it's the mozilla dev page reference for JavaScript Date: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜