calendar

calendar is a javascript library featurering functions for calculating dates. The library makes use of julian day when doing the calculations. So far, the library only supports iso 8601. The library is in use in the CMS Fundanemt and websites featuring a date selecter.

Please note that the library only support date calculations in the gregorian calender and therefore only supports date calculations made on dates after October 15th 1582.

Download: calendar.zip (24.09kb)

Files in calendar.zip

FileDescription
/calendar.jsThe calendar library itself. To be used alone or with a language plug-in. When using the functions mkMonthView() or mkCalendarView(), a language plugin is required.
/calendar_da.jsThe Danish language plug-in - this plug-in features Danish holidays and other specifically Danish date information.
/calendar_en.jsThe English language plug-in - not yet entirely done - I need help :)
/calendar_de.jsGerman language plug-in - not yet done.
/calendar.cssExample CSS file to be used with the script. CSS is only needed if mkMonthView() or mkCalendarView() is used.
/calendar.htmlAll examples shown on this page.
/da.gifimage used to mark birthdays ( don't forget those! ) in calendar.css.
/heart.gifImage used to mark wedding anniversaries ( don't forget those either!!! ) in calendar.css.
/money.gifImage used to mark paydays in calendar.css.
/gpl_da.txtGnu General Public License 2 - Danish translation.
/gpl_en.txtGnu General Public License 2 - Original version.

Functions

Below, you will find examples of the most important functions with brief descriptions.

compareDates()

compareDates() compares two dates and returns true if they are similar. The comparison is made with no regard to the time of day. Therefore 2004-02-28T22:22 and 2004-02-28T10:10 will yeld true. The function accepts two arguments. If the last arguments is omitted, the date of today is used for the comparison.

Syntax

compareDates(float jd1[,float jd2]);

Example:

<script type="text/javascript">
    if(compareDates(mkjd(2006,10,22))) {
        document.write("Today is october 22. 2006.");
    } else {
        document.write("It is not  october 22. 2006 today.");
    }//else
</script>

Result:

getEaster()

getEaster() returns the Julian day Easter occours in the selected year.

Syntax

getEaster([float jd]);

Example:

<script type="text/javascript">
    var months = new Object();
    months["3"] = "march";
    months["4"] = "april";
    var nextYear = jd2date(mkjd())["year"] + 1;
    var easter = jd2date(getEaster(mkjd(nextYear)));
    document.write("Next year Easter falls on ");
    document.write(months[easter["month"]] + " the " + easter["day"]);
</script>

Result:

getWeek()

getWeek returns a ISO 8601 formatted week number for a given date. That is a number between 1 and 53.

Syntax

getWeek([float jd]);

Example:

<script type="text/javascript">
    document.write(getWeek());
</script>

Result:

getWeekDay()

getWeekDay() returns the day of week, where 0 is Sunday and 6 is Saturday.

Syntax

getWeekDay([float jd]);

Example:

<script type="text/javascript">
    var weekdays = new Array(
        "sunday","monday","tuesday","wednesday","thursday","friday","saturday"
    );
    document.write("Today is " + weekdays[getWeekDay()] + ".");
</script>

Result:

isLeapYear()

isLeapYear() examines whether a given year is a leap year or not. If the given year is a leap year, true is returned.

Syntax

isLeapYear([float jd]);

Example:

<script type="text/javascript">
    document.write("The year of 2003 was ");
    if(!isLeapYear(mkjd(2003))) document.write("not ");
    document.write("a leapyear.");

    document.write("The year of 2004 was ");
    if(!isLeapYear(mkjd(2004))) document.write("not ");
    document.write("a leapyer.");
</script>

Result:

jd2date()

jd2date() converts a Julian day into an array holding information about the date.

Syntax

jd2date(float jd);

Example:

<script type="text/javascript">
    var date = jd2date(2452570.432199074);
    document.write('date["year"] = ' + date['year'] + '<br>');
    document.write('date["month"] = ' + date['month'] + '<br>');
    document.write('date["day"] = ' + date['day'] + '<br>');
    document.write('date["hour"] = ' + date['hour'] + '<br>');
    document.write('date["minute"] = ' + date['minute'] + '<br>');
    document.write('date["second"] = ' + date['second'] + '<br>');
    document.write('date["intweekday"] = ' + date['intweekday']);
</script>

Result:

mkCalendarView()

mkCalendarView() creates a visual calender that can be used anywhere. This function is actually meant as an example of using the calendar class as it makes use of almost all of the functions.

Syntax

mkCalendarView([float jd[,string id[,string locale[,string actionC]]]])

Example:

<div style="width:400px;">
<div id="calendar_desc"
    style="border:solid 1px #000000;
           padding-left:10px;
           float:left;
           width:200px;
           background-color:#ffffff;
           color:#000000;"> </div>
<div id="calendar" style="padding-left:30px;"></div>
</div>
<script type="text/javascript">
    
    var entries = new Object();

    entries[1] = new Object();
    entries[1]["date"]  = "Y-8-22";
    entries[1]["short"] = "Anjas birthday";
    entries[1]["long"]  = "18.00 Guests invited for supper.";
    entries[1]["class"] = "calM";

    entries[2] = new Object();
    entries[2]["date"]  = "Y-6-16";
    entries[2]["short"] = "Christians birthday";
    entries[2]["long"]  = "18.00 Guests invited for supper.";
    entries[2]["class"] = "calM";

    entries[3] = new Object();
    entries[3]["date"]  = "Y-10-7";
    entries[3]["short"] = "Weddingday";
    entries[3]["long"]  = "18.00 We are going to a good restaurent.<br>";
    entries[3]["long"] += "21.00 In the movie theater.<br>";
    entries[3]["class"] = "calW";

    entries[4] = new Object();
    entries[4]["date"]  = "Y-M-1";
    entries[4]["short"] = "Payday";
    entries[4]["class"] = "calP";

    entries[5] = new Object();
    entries[5]["date"]  = "2006-10-12";
    entries[5]["short"] = "Nice evening";
    entries[5]["long"]  = "14.00 off from work.<br>";
    entries[5]["long"] += "18.00 table reserved at restaurent.<br>";
    entries[5]["long"] += "21.00 In the movie theater.<br>";
    entries[5]["class"] = "calE";


    function writeInfo(jd) {

            var data = jd2date(jd);
            var description = new Object();

            var c = 1;
            while(c > 0) {
                try {
                    if(entries[c]["date"]) {
                        if(entries[c]["long"]) {
                            var thisdate = entries[c]["date"].replace(/Y/,data["year"]);
                            thisdate = thisdate.replace(/M/,data["month"]);
                            description[thisdate] = entries[c]["long"];
                        }//if
                        c++;
                    } else c = 0;
                } catch(e) {
                    c = 0;
                }//catch
            }//while


            var key = data["year"] + "-" + data["month"] + "-" + data["day"];
            if(description[key]) {
                document.getElementById("calendar_desc").innerHTML = description[key]


            } else {
                document.getElementById("calendar_desc").innerHTML = "Ingen aftaler";
            }
    }//writeInfo

    var c = 1;
    while(c > 0) {
        try {
            if(entries[c]["date"]) {
                mySpecialDays[entries[c]["date"]] = entries[c]["short"] + "|" + entries[c]["class"];
                c++;
            } else c = 0;
        } catch(e) {
            c = 0;
        }//catch
    }//while

    mkCalendarView(mkjd(2006,10,10),"calendar","en","writeInfo");
</script>

Resultat:

 

mkjd()

mkjd() creates a Julian day.

Syntax

mkjd([integer year[, integer month[,integer day[,integer hour[,integer minute[,integer second]]]]]]);

Example:

<script type="text/javascript">
    document.write(mkjd(2002,10,22,22,22,22));
</script>

Result:

validDate()

Validates a date. If the date exists, true is returned. Otherwise, false is returned.

Syntax

validDate(integer year,integer month,integer day);

Example:

<script type="text/javascript">
    document.write("February the 29th 2002 is ");
    if(!validDate(2002,2,29)) document.write("not ");
    document.write("a valid date.<br>");
    
    document.write("February the 28th 2002 is ");
    if(!validDate(2002,2,28)) document.write("not ");
    document.write("a valid date.<br>");
</script>

Result: