Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

display slides with start and end dates with javascript

I am new to javascript and wondering if there is a way in a slider to display individual slides with start and end dates, for example:

I have multiple slides that need to be visible on dates and hidden on others Slide 01 : visible Nov 5 - Nov 8 (hidden after the 8th) Slide 02 : visible Nov 6- Nov 10 slide 03 : visible Nov 8 - Nov 10 and so on.....

Ive tried to achieve this by using window.setInterval(function() and using variables with if and getTime() but I'm not sure if this is the best way to do this. Thank you in advance for any input anyone has on this subject.

Hi David,

I'm trying to clarify your question. You want to have a slider that loads a set of slides according to the current date?

Something like this:

Set 1 - image1, image2, image3 - visible from 6 to 10 of November. Set 2 - image4, image5, image6 - visible from 11 to 15 of November.

And so on.

Hugo thank you for your response. That is kind of the idea I guess I can explain better this way:

Day 01 - slide 1, slide 2 Day 02 - slide 1, slide 2, slide, 3 Day 03 - slide 1, slide 2, slide 3, slide 4 ...and so on until you get to 6 days with 6 slides. Its a new slide each day with the slides from the previous day still loading.

In addition, after those specified set of dates, those slides would disappear and a new set would load.

Hi David, You didn't rely explain what you are "stuck" on, but maybe it's the timer for counting days.

The magic number is "86400" as in: days = Math.floor(seconds / (60*60*24)); or longhand: days = Math.floor(seconds / (60*60*24));

here's some links: Easy Countdown to Date with JavaScript & jQuery http://trulycode.com/bytes/easy-countdown-to-date-with-javascript-jquery/

Real time display of time left for an event in days, hours, minutes and seconds http://www.plus2net.com/javascript_tutorial/countdown.php

HTML Countdown to Date v3 JavaScript Timer (page includes interactive script generation) http://www.ricocheting.com/code/javascript/html-generator/countdown-timer

You can even adjust for selectable time zones: http://www.rmkwebdesign.com/Countdown_Timers/Samples.php

Hope this gets you started..

Thanks James, I actually already have a countdown timer, what Im looking for is a bit different, which is for divs to be shown on specific dates within a slider, and hidden on others. I really appreciate the info you shared though. I got something working using the following code:

window.setInterval(function(){

var current = new Date(); var normal = new Date("November 6, 2014 00:00:00"); var countdown = new Date("November 20, 2014 00:00:00"); var day1 = new Date("November 24, 2014 00:00:00"); var day2 = new Date("November 25, 2014 00:00:00"); var day3 = new Date("November 26, 2014 00:00:00"); var day4 = new Date("November 27, 2014 00:00:00"); var blackfriday = new Date("November 28, 2014 00:00:00"); var postblackfriday = new Date("November 29, 2014 00:00:00"); var cybermonday = new Date("December 1, 2014 00:00:00"); var postmonday = new Date("December 2, 2014 00:00:00");

// Normal Slider if(current.getTime()>normal.getTime()){ $('.blackfridayslider').hide(); $('.normal').show(); $('.blackfridaystore').hide(); $('.normalstore').show(); $('.normalstorenav').show(); $('.blackfridaystorenav').hide(); }

I was having some issues with IE8, but I just figured out that it was related to an html5 video that was playing as an intro prior to the slider loading.

1 Answer

So I figured out a way to accomplish what I was trying to do, it may not be the best way but it works

window.setInterval(function(){

var current = new Date(); var normal = new Date("November 6, 2014 00:00:00"); var countdown = new Date("November 20, 2014 00:00:00"); var day1 = new Date("November 24, 2014 00:00:00"); var day2 = new Date("November 25, 2014 00:00:00"); var day3 = new Date("November 26, 2014 00:00:00"); var day4 = new Date("November 27, 2014 00:00:00"); var blackfriday = new Date("November 28, 2014 00:00:00"); var postblackfriday = new Date("November 29, 2014 00:00:00"); var cybermonday = new Date("December 1, 2014 00:00:00"); var postmonday = new Date("December 2, 2014 00:00:00");

// Normal Slider if(current.getTime()>normal.getTime()){ $('.blackfridayslider').hide(); $('.normal').show(); $('.blackfridaystore').hide(); $('.normalstore').show(); $('.normalstorenav').show(); $('.blackfridaystorenav').hide(); }

The only issue I'm running into is with IE8 and an intro video that runs on the first date not working. Thanks everyone for your input! If anyone knows of a better way to do this I would love some more input as I am very new to JS!