Hi all,
I am re-posting my original JS date function and some new Date functions. These
functions convert a date or a "day number" to and from each other. This allows for
some date math, like adding a number of days to a date and see what the resulting date
is.
The original code I posted used the function DaysFromZero to convert the date into a
day number. There seems to be a "standard" for the day number; it's called "Julian
Day". "Julian Day" or JD is not named for Julian Caesar, as the "Julian Calendar" is,
but rather for an Astronomer. To convert the function DaysFromZero to create a JD, all
I need to do is to add 1720999 days to the resulting number from the original
DaysFromZero function.
The decimal portions of the JD number are the time at GMT/UTC as a decimal portion of
the day. That is 1 sec = 0.0000115740740740740740740740741 JD
(1 sec= 1/86400 JD, where 86400 = 24*60*60, that is the number of seconds in a day). I
have not yet built any methods to handle or deal with time or partial JD.
Also, I have found it somewhat simpler to express the Gregorian leap year rule(s) as
parseInt(365.2425 * yy)
rather than
parseInt(365.25 * yy) - parseInt(yy/100) + parseInt(yy/400)
I haven't yet bothered to create a date object and make these functions as methods of
the object, but you are free to do so if you'd like. Some of this code is simply me
playing with and learning JavaScript objects, sorry if this confuses you.
Anyway, these are all now yours to enjoy!!
<SCRIPT language="JavaScript">
function JDtoDate (JD) {
//define some local vars
var a = new Number();
var b = new Number();
var c = new Number();
var m = new Number();
var d = new Number();
var y = new Number();
// do the math for the conversion
a = Math.round(JD) + 68569;
b = Math.floor((4 * a)/ 146097);
a = a - Math.floor(((146097.0 * b) + 3) / 4);
c = Math.floor((4000 * (a + 1))/ 1461001);
a = a - Math.floor((1461 * c) / 4) + 31;
m = Math.floor((80 * a) / 2447);
d = a - Math.floor((2447 * m) / 80);
a = Math.floor(m / 11);
m = m + (2 - (12 * a));
y = (100 * (b - 49)) + c + a;
//expose the results as properties
this.year = y;
this.month = m;
this.day = d;
// Create a string to return when no method or property of this object is used
var StringToReturn = this.year + '-' + this.month + '-' + this.day;
// create a default toString method
this.toString = function () {
return StringToReturn;
};
// create a default valueOf method
this.valueOf = function () {
return JD;
};
};
function DateToJD (yy,mm,dd) {
if ( mm > 2) {
mm = mm + 1;
} else {
mm = mm + 13;
yy = yy - 1;
};
var i = parseInt(365.2425 * yy) + parseInt(30.6 * mm) + dd + 1720997;
return i;
};
var xx = DateToJD(2003,3,3);
alert('Julian Day Number = ' + xx);
var MyDate = new JDtoDate(xx);
alert('New Date = ' + MyDate.year + '-' + MyDate.month + '-' + MyDate.day)
var MyDate = new JDtoDate(xx + 10);// the original date + 10 days
alert('MyDate = ' + MyDate ); // uses the "valueOf()" method
var MyDate = new JDtoDate(xx - 10);// the original date - 10 days
alert(MyDate); // uses the "toString()" method
</SCRIPT>
<SCRIPT language="JavaScript">
function daysBetweenDates () {
var StartYear = new Number(1900);
var StartMonth = new Number(1);
var StartDay = new Number(1);
var EndYear = new Number(1970);
var EndMonth = new Number(1);
var EndDay = new Number(1);
// static method, daysBetweenDates must be invoked as an object to use
this.DateToJD = function (yy,mm,dd) {
if (mm > 2) {
mm = mm + 1;
} else {
mm = mm + 13;
yy = yy - 1;
};
var i = parseInt(365.2425 * yy) + parseInt(30.6 * mm) + dd + 1720997;
return i;
};
var EndJD = DateToJD(EndYear,EndMonth,EndDay);
var StartJD = DateToJD(StartYear,StartMonth,StartDay);
return (EndJD-StartJD+1);
};
alert ('Days Between Dates = ' + daysBetweenDates());
// use Static method, initiate the "daysBetweenDates" object first
var bb = new daysBetweenDates();
alert ('daysBetweenDates.DateToJD = ' + bb.DateToJD(2003,3,3) + ' vs. ' +
DateToJD(2003,3,3));//compare with DateToJD function above
</SCRIPT>
________________________________________________________________________
TO UNSUBSCRIBE: send a plain text/US ASCII email to [EMAIL PROTECTED]
with unsubscribe witango-talk in the message body