[ create a new paste ] login | about

Link: http://codepad.org/it10aljf    [ raw code | fork ]

C, pasted on Oct 19:
<script language="javascript">

function cal()
{

// so older browsers are supported
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

document.body.innerHTML = "";

var monthObj = {
    "Jan" : 31,
    "Mar" : 31,
    "Apr" : 30,
    "May" : 31,
    "Jun" : 30,
    "Jul" : 31,
    "Aug" : 31,
    "Sep" : 30,
    "Oct" : 31,
    "Nov" : 30,
    "Dec" : 31
};

var mydate=new Date();

var myyear=mydate.getFullYear();

if (myyear%4 == 0)
{
    if (myyear%100 == 0)
    {
        if (myyear%400 == 0) 
        {
            var leap = 1;
        }
        else
        {
            var leap = 0;
        }
    }
    else
    {
        var leap = 1;
    }
}
else
{
    var leap = 0;
}
    
if (leap == 1)
{
    monthObj["Feb"] = 29;
}
else
{
    monthObj["Feb"] = 28;
}

var months=new Array("Jan","Feb","Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

var daysinweek=new Array("Mon","Tue","Wed","Thu","Fri","Sat", "Sun");

var currmth=mydate.getMonth();

currmth=currmth+1;

if (currmth == 12)
{
    var nextmth = "Jan";
    var newyr = myyear + 1;
}
else
{
    var nextmth = months[currmth];
}

if (typeof lastday != "undefined")
{
    var month = nextmth;
    var date = 1;
    if (lastday == "Sun")
    {
        var firstday = "Mon";
    }
    else
    {
        var dayk = daysinweek.indexOf(lastday);
        var firstday = daysinweek[dayk+1];
    }
    var dayofweek = firstday;
}
else
{
    var month = months[currmth-1];
    var date = mydate.getDate();
    var dayofweek = daysinweek[mydate.getDay()];
}

if (typeof newyr != "undefined")
{
    var year = newyr;
}
else
{
    var year = mydate.getFullYear();
}

var tmpdate = date;
var dist = 0;
var cal = new Array();
var key = daysinweek.indexOf(dayofweek);

if (tmpdate != 1)
{
    while (tmpdate != 1)
    {
        tmpdate--;
        dist++;
    }
    tmpdate = date-1;
    key = key-1;
    while (dist > 0)
    {
        if (key == -1)
        {
            key = 6;
        }
        cal[tmpdate] = daysinweek[key];
        key--;
        tmpdate--;
        dist--;
    }
    cal[date] = dayofweek;
    tmpdate = date + 1;
    key = daysinweek.indexOf(dayofweek) + 1; //check
    while (tmpdate <= monthObj[month])
    {
        if (key == 7)
        {
            key = 0;
        }
        cal[tmpdate] = daysinweek[key];
        key++;
        tmpdate++;
    }
}
else
{
    key = daysinweek.indexOf(dayofweek);
    cal[date] = dayofweek;
    date = date+1;
    key = key+1;
    while (date <= monthObj[month])
    {
        if (key == 7)
        {
            key = 0;
        }
        cal[date] = daysinweek[key];
        key++;
        date++;
    }
}

document.write("<table><tr><td align='center' colspan=7>" + month + " " + year + "</td></tr><tr><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td><td>S</td></tr>");
key = 1;
var posinweek = 0;
var tr = 0;

while (key <= monthObj[month])
{
    if (posinweek == 0)
    {
        document.write("<tr>");
    }
    if (posinweek == 7)
    {
        document.write("</tr>");
        tr = tr + 1;
        posinweek = 0;
    }
    if (posinweek <= 6)
    {
        if (cal[key] == daysinweek[posinweek])
        {
            document.write("<td>" + key + "</td>");
            key++;
            posinweek++;
        }
        else
        {
            document.write("<td></td>");
            posinweek++;
        }
    }
}

if (tr != 4)
{
    for(var i = 6; posinweek <= i; posinweek++)
    {
        document.write("<td>&nbsp;</td>");
    }
}
else
{
    posinweek = 0;
    document.write("<tr>");
    for (var i = 6; posinweek <= i; posinweek++)
    {
        document.write("<td>&nbsp;</td>");
    }
    document.write("</tr>");
}

document.write("</tr><tr>");

var lastday = cal[monthObj[month]];
var newyr = 1;

if (typeof lastday != "undefined")
{
    document.write("<td align='left' colspan=7><a href='javascript:void(0)' onclick='function(){cal()}'>Prev</td>");
}
else
{
    if (nextmth == "Jan")
    {
        document.write("<td align='left' colspan=7><a href='javascript:void(0)' onclick='function(){cal(lastday, newyr)}'>Next</td>");
    }
    else
    {
        document.write("<td align='left' colspan=7><a href='javascript:void(0)' onclick='function(){cal(lastday)}'>Next</td>");
    }
}

document.write("</tr>");
document.write("</table>");

}

window.onload = function(){ cal(); }

</script>


Create a new paste based on this one


Comments: