Tutorials at Mhkonline js Index

Get Day (Find out what day it is!)

Javascript 1.2
Intermediate
29 Sep 2006
***

Demo

Description

  There are a number of ways to calculate what day a given day will be. Here we are using the Julian method. We have also provided an implementation tutorial for this program using C programming language. The web centered program might have more use and portability but Intermediate C programmers would be able to understand the program with ease.

  The theory behind this is that we consider January 1st of every year to be Julian Day 1. December 31st will be Julian day 366 for a leap year and 365 for other years. We first find out the Julian day corresponding to the day in question. We calculate the day number using the equation

( julian_day + year + fours - hundreds + four_hundreds)%7

where (each of these are whole numbers)

year = the year in question
fours=(year-1)/4
hundreds = (year-1)/100
four_hundreds = (year-1)/400

  The result of the equation will be a whole number between 0 and 6. If it is 0, the day in question is a Saturday. if it is 1, Sunday and so on. Identifying this and getting the equivalent day is an easy programming task. The main functions in Javascript implementation is given below. Copy and paste it in the head section of the document.

<script language="javascript1.2">
<!--
/*(c) 2006 Centrum inc Software Solutions R & D
* Calculating the day using Julian Equation */

var dates = new Array();
dates[0] = 'Saturday';
dates[1] = 'Sunday';
dates[2] = 'Monday';
dates[3] = 'Tuesday';
dates[4] = 'Wednesday';
dates[5] = 'Thursday';
dates[6] = 'Friday';

function getTheDate(form){    // Gets the data from the form and calculates the day
    var isValid = 1,theDate = '';
    with(form){
        var theDay = m_day.value;
        var theYear = m_year.value;
        var theMonth= m_month.value;
    }
    if(isNaN(theDate))isValid = 0;
    if(isNaN(theYear) || theDate<0 || theYear.length==0)isValid = 0;
    if(isValid){
var isLeap = 0;
if((theYear%4 == 0)&&(theYear%100 != 0)||(theYear%400 == 0)) isLeap = 1;
var julianDay = getDays(isLeap,theMonth,theDay);
var year = theYear;
var fours = Math.floor((year - 1)/4);
var hundreds = Math.floor((year - 1)/100);
var four_hundreds = Math.floor((year - 1)/400);
var t1 = (Math.round(year) + Math.round(julianDay) + Math.round(fours) - Math.round(hundreds) + Math.round(four_hundreds))%7;
theDate = dates[t1];
    }else theDate = '<span style="color:#ff3300;">Invalid Choice!</span>';
    document.getElementById('m_result').innerHTML = theDate;
}

function getDays(isLeap,month,day){    // Returns the no of days till the date in qn
    var i, julDays = 0;
    for(i = 1; i<month; i++) {
        if((i==1)||(i==3)||(i==5)||(i==7)||(i==8)||(i==10)||(i==12))
            julDays += 31;
        else if((i==2)&&(isLeap==1))julDays += 29;
        else if((i==2)&&(isLeap==0))julDays += 28;
        else julDays += 30;
    }
var days = Math.round(julDays) +Math.round(day);
return days;
}

Insert this code for the form where you want to display the form.

<form name="m_date" onsubmit="return false;">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="13%" align="center"><label>Date&nbsp;</label><input name="m_day" type="text" size="4" maxlength="2" /></td>
<td width="21%" align="center"><label>Year </label><input name="m_year" type="text" size="6" maxlength="4" /></td>
<td width="28%"><label>Month</label>
<select name="m_month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select> </td>
<td width="12%"><input type="submit" name="Submit" value="Get Date" onclick="getTheDate(this.form);" /></td>
<td width="26%"><div id="m_result" style="width:20px;"></div></td>
</tr>
</table>
</form>

Thats it. If you give the correct input the script will display the correct day. We first implemented this algorithm in C, but when we tried to convert it into Javascript, we were faced by a number of problems. The major one being the '+' operator concatenating numbers instead of adding them. We put the numbers inside a Math.round() function to make sure that they were summed as numbers.

Feel free to contact us if you have any doubts or clarifications.

Tutorials Navigation

C C++
Css
Flash
Fireworks
Javascript


Random Tutorials