JRS: In article <
[email protected]
..net>, dated Wed, 10 Nov 2004 04:23:20, seen in
news:comp.lang.javascript said:
// compares dates to see if they are the same day.
//(Only interested in local dates.)
Call it AreDatesSame, and the effect is more obvious.
function compareDates(date01, date02){
if(date01.getDate()!=date02.getDate()){
return false;
}else{
if(date01.getMonth()!=date02.getMonth()){
return false;
}else{
if(date01.getYear()!=date02.getYear()){
return false;
}else{
return true;
}
}
}
}
I think that this is equally effective :
function compareDates(date01, date02) {
return date01.getDate() == date02.getDate() &&
date01.getMonth() == date02.getMonth() &&
date01.getYear() == date02.getYear() }
Another approach would be to write a function (or method) to convert the
milliseconds UTC which is actually (it seems) stored to a local
daycount; your holiday table could be pre-converted in a single pass.
function LDC(D) {
return Math.floor( (+D + D.getTimezoneOffset()*6e5)/864e5 ) }
where the second + might need to be - . Test that.
function printDueDate(mydate, branch_index, due){
xdayslater.setYear(mydate.getYear());
xdayslater.setMonth(mydate.getMonth());
xdayslater.setDate(mydate.getDate());
addDays(xdayslater, Loan_Periods[due] );
while(isBranchClosed(xdayslater, Branch_Names[branch_index]) ){
addDays(xdayslater, 1);
}
document.write( Month_Names[xdayslater.getMonth() ] + " " +
xdayslater.getDate() + ", "+ isBranchClosed( xdayslater, "DO") );
}
There, xdayslater must be a Date Object of uncertain initial value; if
it happens to be the 31st of a month and setMonth calls for Sep Apr Jun
Nov, or the 31st, 30th, and probably 29th and setMonth calls for Feb,
then the result will not be as probably wanted.
One can do setFullYear(Y, M, D).
Better to declare xdayslater as local,
var xdayslater = new Date(0)
where the initialisation is fast /* (0) beats () */ and sets, throughout
the Old World, 1970-01-01 local. (It's actually wrong for the UK, since
we used GMT+1 on that date.)
var xdayslater = new Date(2e8)
should set a safe date everywhere; early Jan 1970.
BUT : If mydate is to be copied into a new Object,
datecopy = new Date(+mydate)
should be best.
ISTM that you have no interest in time of day. If you use setHours to
put a Date Object to noon, you can then gaily add and subtract multiples
of 864e5 without worrying about Summer Time changes, which then cannot
affect Y M D.
Functions such as addDays are frequently wrong, by ignoring Summer Time.
Works well, except for a few places wwhere the due dates written are
days that the location is closed. The while loop should have iterated
one more day. See this page:
http://pjpf1.home.att.net/duedatetester_DO.html
I read News off-line.
Note the One Week loan days for the 18th and 19th are the 27th - which
is a weekend day where it is normally closed.
You did not say what the month, or location, was; but from what you
posted here that *could* be a Summer Time problem.
I think that you would do best to convert your holiday table, which is
apparently used repeatedly, from YMD to a local integer daycount, and to
work entirely with daycounts. If your business does not get too near
the Date Line (AK & HI are safe; NZ etc. are not) then you can safely
convert daycount+0.5 to local date with the usual functions
with (new Date((dc+0.5)*864e5) ... getDate() ...
Otherwise, IIRC,
with (new Date(d*864e5) ... getUTCDate() ...
Note that it is sufficiently easy to convert Y M D to/from a daycount
without using Date Objects; it can also be faster : see in
<URL:
http://www.merlyn.demon.co.uk/daycount.htm>