Skip to main content
csm_phil
Legend
September 25, 2011
Question

Date & Day & Month please help

  • September 25, 2011
  • 2 replies
  • 1061 views

Hi Scripters,

I have creat a diary book in InDesign CS4.

Probelm: I want to alert the date and day for each time like this. But after the 7 saturday after alert the 8th day is undefined result.

Date     Day

1          Sunday

2          Monday

3          Tuesday

4          Wednesday

5          Thursday

6          Friday

7          Saturday          This is alert fine after the date execute in 8 i want to alert Day "Sunday" but i get it result in "undefined"

Please check my Below JS Code Give your valuable output.

var myMonth= new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

var myDay = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

var myThirty = new Array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30");

var myThirtyfirst = new Array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");

var myFeb = new Array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28");

for(i=0; i<myMonth.length; i++){

    if(myMonth=="January"){

        for(j=0; j<=myThirtyfirst.length; j++){

            alert(myThirtyfirst+"|"+myDay);

               // after the 8 day is alert is shown  in undefined; i want to continue after the next month is started in wednesday or some other day please check

            }

        }

    else if(myMonth=="February"){

        for(j=0; j<=myFeb.length; j++){

            alert(myFeb+"|"+myDay);

            }

        }

    else if(myMonth=="March"){

        for(j=0; j<=myThirtyfirst.length; j++){

            alert(myThirtyfirst+"|"+myDay);

            }

        }

    }

Note: Saturday & Sunday will come in one alert. Definitely i am confused little bit think so.

thx

csm_phil

This topic has been closed for replies.

2 replies

Inspiring
September 26, 2011

Hi csm_phil,

Another problem in your code is that you're counting one too many in a number of spots. If you're counting from 0, you can only go to length-1 - so

for(j=0; j<=myThirtyfirst.length; j++){

should be.

for(j=0; j<myThirtyfirst.length; j++){

The way I generally code it is, depending on what I am counting: if I count from 0, I use < when comparing the counter to the length; if I start from 1, I use <= when comparing your counter to the length.

Now, independent of that, your code is overly complex - as the other posters already said: at least check out the built-in Date object, or that cool Date library Marc referred us to.

Date and time calculations are very tricky to get right, and lucky for us there is no need to reinvent the wheel!

csm_phil
csm_philAuthor
Legend
September 27, 2011

Hi,

Thanks for all to share the good valuable solutions and corrections to me.

thx

csm_phil

September 26, 2011

You know that Javascript has a built in Date object, right? Here's the first result on google.

http://www.w3schools.com/js/js_obj_date.asp

In answer to your question. If you ask for an index in an array that is not defined, you will get "undefined". Arrays don't wrap around. You can use myDay[j%7] to wrap around.

Marc Autret
Legend
September 26, 2011

See also:

http://www.datejs.com/

@+

Marc

Harbs.
Legend
September 26, 2011

Marc, that is very, very cool!