Solved

# Javascript Auto Date Change On Specific Weekday

Posted on 2012-09-09
760 Views
Experts,

I need a script that will only change the date on Wednesday and it has to be 30 days in advance.  In other words on Wednesday, September 12th it will show Wednesday, October 10th.  The only time the date changes is on the following Wednesday.  So from Thursday to Tuesday it will still show Wednesday, October 10th.  Then on Wednesday, September 19th the date will conversely change to Wednesday, October 17th and again show that date throughout the week until the following Wednesday is reached when it will then change to Wednesday, October 24th.

The script below almost gets me there but it changes the date daily and as previously mentioned I need it to only change on Wednesday.

What am I missing to make this happen?  If there's a better way to write this, I'm open to suggestions.

<script type="text/javascript">

// Get today's current date.
var now = new Date();

// Array list of days.
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

// Array list of months.
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

// Calculate the number of the current day in the week.
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

// Calculate four digit year.
function fourdigits(number)      {
return (number < 1000) ? number + 1900 : number;
}

// Join it all together
weekday =  days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear())) ;

// Print out the data.
document.write(weekday);

</script>
0
Question by:JesusSardinas

LVL 12

Expert Comment

One way to handle this is by varying the number of days to add according to the day of the week.

var day=now.getDay();

switch (day)
{
case 3: var NUM_OF_DAYS_TO_ADD = 30; break;
case 4: var NUM_OF_DAYS_TO_ADD = 29; break;
case 5: var NUM_OF_DAYS_TO_ADD = 28; break;
case 6: var NUM_OF_DAYS_TO_ADD = 27; break;
case 0: var NUM_OF_DAYS_TO_ADD = 26; break;
case 1: var NUM_OF_DAYS_TO_ADD = 25; break;
case 2: var NUM_OF_DAYS_TO_ADD = 24; break;
}

Also, is there a reason you're using function fourdigits rather than the built-in .getFullYear()?
0

Author Comment

Thanks Amick.  I'm a server guy and just learning the coding side as I have been thrown into it at work.  That should give you an idea why I'm not using .getFullYear().  I didn't know about it ;)

I'll research it to learn how to properly code that line.

I adjusted the number of days to add so it lines up with Wednesday and will check in the morning to see if it still shows the same date before closing out this request.

Thanks for the assistance!
0

LVL 12

Accepted Solution

This ought to do it:

// Join it all together
weekday =  days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " + now.getFullYear() ;

One other thing that you may have noticed is that 30 days after Wednesday, September 12 is Friday, October 12, but it is only  28 days to Wednesday, October 10.

If you are going for 30 days, leave the switch code as is, but if you are going for the fourth Wednesday in the future, you'll need to add 28 days rather than 30, therefore you'll have to adjust  as follows:

switch (day)
{
case 3: var NUM_OF_DAYS_TO_ADD = 28; break;
case 4: var NUM_OF_DAYS_TO_ADD = 27; break;
case 5: var NUM_OF_DAYS_TO_ADD = 26; break;
case 6: var NUM_OF_DAYS_TO_ADD = 25; break;
case 0: var NUM_OF_DAYS_TO_ADD = 24; break;
case 1: var NUM_OF_DAYS_TO_ADD = 23; break;
case 2: var NUM_OF_DAYS_TO_ADD = 22; break;
}

Good luck with your project, and let me know if there's anything more I can do to help.
0

Author Comment

Thanks again Amick for your support.  The switch day code appears to have worked!
0

## Featured Post

### Suggested Solutions

Both jQuery and Mootools offer some nice, easy to use scripts. Mootools is best known for their interactive slideshows and jQuery for their menus, but both have many other uses as well.   jQuery is basically a toolkit with many DOM (http://en.wik…
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved." Ralph Waldo Emerson Introduction: One of the wonderful things about the web is that it makes it s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…