?
Solved

Javascript Auto Date Change On Specific Weekday

Posted on 2012-09-09
4
Medium Priority
?
767 Views
Last Modified: 2012-09-10
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.

Thanks in advance!




<script type="text/javascript">


// Get today's current date.
var now = new Date();
 
var NUM_OF_DAYS_TO_ADD = 30;
 
now.setDate(now.getDate() + NUM_OF_DAYS_TO_ADD);


// 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
Comment
Question by:JesusSardinas
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Amick
ID: 38381531
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

by:JesusSardinas
ID: 38381549
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

by:
Amick earned 2000 total points
ID: 38382789
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

by:JesusSardinas
ID: 38382897
Thanks again Amick for your support.  The switch day code appears to have worked!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question