开发者

Populate Image Based on Start Date / End Date

I will try to be as specific as possible. I've completely confused myself with everything I've tried, so hopefully someone will be able to help me out.

I have a list of images that all have certain start and end dates for which they should be displayed on the page. Before I continue, I am looking for a strictly Javascript/jQuery solution; a server-side solution, however possible, is not what I'm asking for.

I use a js example I found somewhere else to find the current date with the following:

var date = new Date();
var d = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
var yearInt = parseInt(year);
var currentDate = month + "/" + day + "/" 开发者_如何学C+ year;

I then use ajax and $.each to go through all of my xml nodes and return all values, including a start date and end date for each image.

What type of conditional should I write so that the images display if they hold the following info:

Current Date: 05/20/2010

Image1: Start Date: 01/30/2010 - End Date: 06/15/2010

Image2: Start Date: 05/20/2010 - End Date: 06/20/2010

Image2: Start Date: 06/12/2010 - End Date: 06/20/2011

And of course hide if they're past the end date?

Any help would be greatly appreciated! I'll answer any questions as quickly as possible. Thanks!


I would recommend casting your dates as Date() objects, you can then do a comparison on them.

var currentDate = new Date(year, month, day);
var startDate = new Date(insert start date);
var endDate = new Date(insert end date);

if(currentDate >= startDate && currentDate < endDate) {
    //show images
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜