www.webdeveloper.com
Results 1 to 7 of 7

Thread: Calendar

  1. #1
    Join Date
    Jun 2013
    Posts
    1

    Calendar

    I would like each of my calendar specialDays to have a link to another location on the web.
    How do I assign a string to my specialDays array or what other methods would be suggested?

    Here is my code:

    <script language="javascript">
    var specialDays=new Array()
    // set special days for links below: specialDays[x]=['month_number', 'date_number']
    specialDays[0]=['6', '27', '2013']
    specialDays[1]=['7', '27', '2013']

    if(specialDays[0]){
    var strSpecialDay='link1';
    }
    if(specialDays[1]){
    strSpecialDay='link2';
    }

    var w3c=(document.getElementById)?true:false;
    var ie4=(document.all && !w3c)?true:false;
    var ie5=(document.all && w3c)?true:false;
    var ns4=(document.layers)?true:false;
    var ns6=(w3c && !document.all)?true:false;
    var mi=0; var yi=0;
    var calA=new Array();
    var cal_m, cal_y, cal, calS, now;

    function setMonth(incr){
    if(!ns4){
    mi+=(incr)?1:-1;
    if(mi>11)mi=0;
    if(mi<0)mi=11;
    now.setMonth(mi);
    updateCalender();
    }}

    function setYear(incr){
    if(!ns4){
    yi+=(incr)?1:-1;
    if(yi>50)yi=0;
    if(yi<0)yi=50;
    now.setFullYear(yi+1970);
    updateCalender();
    }}

    function updateCalender(){
    var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
    dim[1]=(((now.getFullYear()%100!=0)&&(now.getFullYear()%4==0))||(now.getFullYear()%400==0))?29:28;
    cal_m.innerHTML=['January','February','March','April','May','June','July','August','September','October','November',' December'][mi];
    cal_y.innerHTML=yi+1970;
    var offsetD=new Date();
    offsetD.setFullYear(yi+1970);
    offsetD.setMonth(mi);
    offsetD.setDate(1);
    offsetD=offsetD.getDay()+1;
    for(i=1;i<=42;i++){
    if((i-offsetD>=0)&&(i-offsetD<dim[mi])){
    for (var i_tem = 0; i_tem < specialDays.length; i_tem++)
    if ([mi+1]==specialDays[i_tem][0]&&[i-offsetD+1]==specialDays[i_tem][1]&&cal_y.innerHTML==specialDays[i_tem][2]){
    calA[i].innerHTML='<a href="http://myurl/'+strSpecialDay+'.htm">'+[i-offsetD+1]+'</a>';
    break;
    }
    else
    calA[i].innerHTML=i-offsetD+1;
    calA[i].i=i;
    calA[i].o=offsetD;
    calA[i].className=((now.ref.getDate()==i-offsetD+1)&&(now.ref.getFullYear()==now.getFullYear())&&(now.ref.getMonth()==now.getMonth()))?"calTo day":"calText";
    }else{
    calA[i].className="calText";
    calA[i].innerHTML='&nbsp;';
    }}}

    window.onload=function(){
    if(!ns4){
    for(i=1;i<=42;i++)calA[i]=(ie4)?document.all['cal'+i]:document.getElementById('cal'+i);
    cal_m=(ie4)?document.all["calender_m"]:document.getElementById("calender_m");
    cal_y=(ie4)?document.all["calender_y"]:document.getElementById("calender_y");
    cal=(ie4)?document.all["calender"]:document.getElementById("calender");
    calS=(ie4)?document.all["calenderS"]:document.getElementById("calenderS");
    calS.style.height=((ie4||ie5)?cal.clientHeightw3c)?cal.offsetHeight:200)+'px';
    calS.style.width=((ie4||ie5)?cal.clientWidthw3c)?cal.offsetWidth:300)+'px';
    cal.parentNode.style.width=parseInt(calS.style.width)+10+'px';
    cal.parentNode.style.height=parseInt(calS.style.height)+10+'px';
    now=new Date(); now.ref=new Date();
    mi=now.getMonth(); yi=now.getFullYear()-1970;
    updateCalender();
    }

    }
    </script>

    Thank you in advance

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330
    That is a very old script with references like document.all, ns4, ie4 and such.

    Try a google or bing search for javascript calendars for a plethora of newer scripts.
    Even a search of this foum would be fruitful.

    Also, you should enclose your scripts (with HTML) between [ code] and [ /code] tags (without the spaces)
    to make it easier to read, copy, test and return without the emoticons embeded in the forum display.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,151
    I note that you are using a leap year method that is not accurate.

    Try something like this

    Code:
    function isLeapYear(yr){
    
    	return (new Date( yr,1,29 ).getDate()==29 ) ? true : false;
    
    }
    It returns true or false.

    If the date 29th Feb does not exist, the date object automatically rolls over to the 1st March

    An alternative would be
    Code:
    function isLeapYear(x){
    	x = x || new Date().getYear();
    	return ( new Date( x,1,29 ).getDate()==29 ) ? 29 : 28;
    
    }
    Which allows you to get current days of current year by not passing anything but offers the ability to get other years by passing a year.

    So now you can use
    Code:
    var dim=[31,isLeapYear(),31,30,31,30,31,31,30,31,30,31];
    to add in the current years number of days in Feb


    Suggest that you use indentation to help with making your code more readable.

    The function updateCalender(){ has errors in it.

    What are you using to write your scripts? I can suggest Notepad++ which is free and allows you to easily format with indention and colouring of your keywords.

    When posting, please use the appropriate formatting via the code wrapping tags as this also helps people reading your post.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    An alternative to find the last day of a javascript month (from 0 to 11)
    Code:
    function LastDayOfJavascriptMonth(yr,mn){return new Date(yr,mn+1,0).getDate()}
    // use 
    dim=LastDayOfJavascriptMonth(2012,1);
    alert(dim)
    With javascript the last day of a month is always the date 0 of the following month !
    Then there is often no need to function to find this day...
    Last edited by 007Julien; 07-01-2013 at 06:23 AM.

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,151
    I had forgotten that trick, good call.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  6. #6
    Join Date
    Sep 2007
    Posts
    315
    Code:
      
    <script type="text/javascript">
    // http://www.w3schools.com/js/js_obj_date.asp
    
    var dim=[30,28,30,30,30,30,30,30,30,30,30,30];
    var i, t; 
    for(i=0; i< dim.length; i++) {
    t = new Date(2016, i, dim[i]+1);
    // alert(t);
    // alert(  t.getDate() );
    if(t.getDate() != 1 ) { dim[i]++;}
    }
    alert(dim);  // 31,29,31,30,31,30,31,31,30,31,30,31 
    </script>
    The Time Through Ages
    1. By the Time,
    2. Verily Man is in loss,
    3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,151
    Quote Originally Posted by Ayşe View Post
    Code:
      
    <script type="text/javascript">
    // http://www.w3schools.com/js/js_obj_date.asp
    
    var dim=[30,28,30,30,30,30,30,30,30,30,30,30];
    var i, t; 
    for(i=0; i< dim.length; i++) {
    t = new Date(2016, i, dim[i]+1);
    // alert(t);
    // alert(  t.getDate() );
    if(t.getDate() != 1 ) { dim[i]++;}
    }
    alert(dim);  // 31,29,31,30,31,30,31,31,30,31,30,31 
    </script>
    The problem has been solved over the number of days with

    Code:
    function LastDayOfJavascriptMonth(yr,mn){return new Date(yr,mn+1,0).getDate()}
    // use 
    dim=LastDayOfJavascriptMonth(2012,1);
    alert(dim)
    To put it in to a function to return an array with days is simple to do, something like...

    Code:
    function daysArrayForYear(yr){
         yr = yr || new Date().getFullYear(); // if yr not passed, make it current year
         for(c=0,arrayDays=[]; c<11; c++) arrayDays.push( new Date(yr,c+1,0).getDate() );
    return arrayDays;
    }
    // use 
    dim=daysArrayForYear(2016);
    thisyear=daysArrayForYear();
    alert(dim)
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles