473,234 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,234 software developers and data experts.

Can anyhelp me, what does this javascript calendar give error for dates before year 2000?

The function is called from opencalendar(targetfield). Thanks for any
hints on what could be the problem.
.................................................. ............


var decimalPointDelimiter = ".";
var defaultEmptyOK = false;

var dayA = new Array();
var monthA = new Array();
var yearA = new Array();
var targetfield;
monthA[0]="January";
monthA[1]="February";
monthA[2]="March";
monthA[3]="April";
monthA[4]="May";
monthA[5]="June";
monthA[6]="July";
monthA[7]="August";
monthA[8]="September";
monthA[9]="October";
monthA[10]="November";
monthA[11]="December";

for(var i=1;i<=31;i++)
{
dayA[i]=i;
}

for(var i=0;i<10;i++)
{
yearA[i]=gy(i);
}
function gm(num) {
var mydate = new Date();
mydate.setDate(1);
mydate.setMonth(num-1);
var datestr = "" + mydate;
return datestr.substring(4,7);
}

function gy(num) {
var mydate = new Date();
return (eval(mydate.getYear()) - 4 + num);
}

function ud(mon) {
var i = mon.selectedIndex;

if(mon.options[i].value == "2") {
document.forms[0].day.options[30] = null;
document.forms[0].day.options[29] = null;
var j = document.forms[0].year.selectedIndex;
var year = eval(document.forms[0].year.options[j].value);
if ( ((year%400)==0) || (((year%100)!=0) && ((year%4)==0)) ) {
if (document.forms[0].day.options[28] == null) {
document.forms[0].day.options[28] = new Option("29");
document.forms[0].day.options[28].value = "29";
}
} else {
document.forms[0].day.options[28] = null;
}

}

if(mon.options[i].value == "1" ||
mon.options[i].value == "3" ||
mon.options[i].value == "5" ||
mon.options[i].value == "7" ||
mon.options[i].value == "8" ||
mon.options[i].value == "10" ||
mon.options[i].value == "12")
{
if (document.forms[0].day.options[28] == null) {
document.forms[0].day.options[28] = new Option("29");
document.forms[0].day.options[28].value = "29";
}
if (document.forms[0].day.options[29] == null) {
document.forms[0].day.options[29] = new Option("30");
document.forms[0].day.options[29].value = "30";
}
if (document.forms[0].day.options[30] == null) {
document.forms[0].day.options[30] = new Option("31");
document.forms[0].day.options[30].value = "31";
}
}

if(mon.options[i].value == "4" ||
mon.options[i].value == "6" ||
mon.options[i].value == "9" ||
mon.options[i].value == "11")
{
if (document.forms[0].day.options[28] == null) {
document.forms[0].day.options[28] = new Option("29");
document.forms[0].day.options[28].value = "29";
}
if (document.forms[0].day.options[29] == null) {
document.forms[0].day.options[29] = new Option("30");
document.forms[0].day.options[29].value = "30";
}
document.forms[0].day.options[30] = null;
}

if (document.forms[0].day.selectedIndex == -1)
document.forms[0].day.selectedIndex = 0;

}
function showdate() {
var i = document.forms[0].month.selectedIndex;
var j = document.forms[0].day.selectedIndex;
var k = document.forms[0].year.selectedIndex;
alert(document.forms[0].day.options[j].value + "/" +
document.forms[0].month.options[i].value + "/" +
document.forms[0].year.options[k].value)
}
function opencalendar(temptargetfield) {
var field= eval("document.forms[0]." + temptargetfield);
targetfield = "opener.document.forms[0]." + temptargetfield +
".value";
var mydate;
if(field.value == "" || field.value.length < 10 || field.value.length
10)

{
mydate = new Date();
}
else
{
var tempday = field.value.charAt(0).toString() +
field.value.charAt(1).toString();
var tempmonth = (parseInt((parsemonth(field.value.charAt(3).toStri ng(),
field.value.charAt(4).toString())))-1).toString();
var tempyear = (parseInt(field.value.charAt(6).toString() +
field.value.charAt(7).toString() + field.value.charAt(8).toString() +
field.value.charAt(9).toString())).toString();
mydate = new Date(tempyear, tempmonth, tempday );
window.alert(mydate)
}
for(var i=0;i<=monthA.length;i++)
{
if (mydate.getMonth() == i)
{
month = i+1;
}
}
year = mydate.getYear();
//yearA[4].toString();

calwin = window.open("","calwin","resizable,status,height=2 50,width=210");
calccal(calwin,month,year,targetfield);
}
function parsemonth(tmpm1, tmpm2)
{
var m1;
var m2;
var m3;
if (tmpm1 == "0")
{
m1 = "";
}
else
{
m1 = "1";
}
m2 = tmpm2;
m3 = m1.toString() + m2.toString();
return m3;
}
function calccal(targetwin,month,year,targetfield) {
var monthname = new Array(12);
monthname[0] = "January";
monthname[1] = "February";
monthname[2] = "March";
monthname[3] = "April";
monthname[4] = "May";
monthname[5] = "June";
monthname[6] = "July";
monthname[7] = "August";
monthname[8] = "September";
monthname[9] = "October";
monthname[10] = "November";
monthname[11] = "December";

var endday = calclastday(eval(month),eval(year));

mystr = month + "/01/" + year;
mydate = new Date(mystr);
firstday = mydate.getDay();

var cnt = 0;

var day = new Array(6);
for (var i=0; i<6; i++)
day[i] = new Array(7);

for (var r=0; r<6; r++)
{
for (var c=0; c<7; c++)
{
if ((cnt==0) && (c!=firstday))
continue;
cnt++;
day[r][c] = cnt;
if (cnt==endday)
break;
}
if (cnt==endday)
break;
}

targetwin.document.write("<TABLE><TR VALIGN=TOP><FORM>");

var prevyear = eval(year) - 1;
targetwin.document.write("<TD><INPUT TYPE=BUTTON NAME=prevyearbutton
VALUE='<<'"+
" onclick='document.close();opener.calccal(opener.ca lwin,"+month+","+prevyear+",\""+targetfield+"\")'> </TD>");

var prevmonth = calcprevmonth(month);
var prevmonthyear = calcprevyear(month,year);
targetwin.document.write("<TD><INPUT TYPE=BUTTON
NAME=prevmonthbutton VALUE='&nbsp;<&nbsp;'"+
" onclick='document.close();opener.calccal(opener.ca lwin,"+prevmonth+","+prevmonthyear+",\""+targetfie ld+"\")'></TD>");

targetwin.document.write("<TD COLSPAN=3 ALIGN=CENTER>");
var index = eval(month) - 1;
targetwin.document.write("<B>" + monthname[index] + " " + year +
"</B></TD>");

var nextmonth = calcnextmonth(month);
var nextmonthyear = calcnextyear(month,year);
targetwin.document.write("<TD><INPUT TYPE=BUTTON
NAME=nextmonthbutton VALUE='&nbsp;>&nbsp;'"+
" onclick='document.close();opener.calccal(opener.ca lwin,"+nextmonth+","+nextmonthyear+",\""+targetfie ld+"\")'></TD>");

var nextyear = eval(year) + 1;
targetwin.document.write("<TD><INPUT TYPE=BUTTON NAME=nextyearbutton
VALUE='>>'"+
" onclick='document.close();opener.calccal(opener.ca lwin,"+month+","+nextyear+",\""+targetfield+"\")'> </TD>");

targetwin.document.write("</TR><TR>");
targetwin.document.write("<TD>Su</TD>");
targetwin.document.write("<TD>Mo</TD>");
targetwin.document.write("<TD>Tu</TD>");
targetwin.document.write("<TD>We</TD>");
targetwin.document.write("<TD>Th</TD>");
targetwin.document.write("<TD>Fr</TD>");
targetwin.document.write("<TD>Sa</TD>");
targetwin.document.write("</TR>");

targetwin.document.write("<TR><TD COLSPAN=7><HR
NOSHADE></TD></TR>");

var selectedmonth = eval(month) - 1;
var today = new Date();
var thisyear = today.getYear();
var selectedyear = eval(year) - thisyear + 4;

var conditionalpadder = "";

for(r=0; r<6; r++)
{
targetwin.document.write("<TR>");
for(c=0; c<7; c++)
{
targetwin.document.write("<TD>");
if(day[r][c] != null) {
if (day[r][c] < 10)
conditionalpadder = "&nbsp;"
else
conditionalpadder = "";
targetwin.document.write("<INPUT TYPE=BUTTON NAME="+day[r][c]+
" VALUE=" + conditionalpadder + day[r][c] + conditionalpadder +
" onClick=\"window.close();"
+targetfield+"='"+(day[r][c]).toString()+ "/"+
(selectedmonth+1).toString() + "/" + yearA[selectedyear.toString()]
+"';\";>"); }
targetwin.document.write("</TD>");
}
targetwin.document.write("</TR>");
}
targetwin.document.write("</FORM></TABLE>");

}

function calclastday(month,year) {
if ((month==2) && ((year%4)==0))
return 29;

if ((month==2) && ((year%4)!=0))
return 28;

if ((month==1) || (month == 3) || (month == 5) || (month == 7) ||
(month==8) || (month == 10) || (month ==12))
return 31;

return 30;
}

function calcnextmonth(month) {
if (month=="12")
return "1";
else
return (eval(month)+1);
}

function calcnextyear(month,year) {
if (month=="12")
return (eval(year)+1);
else
return (year);
}

function calcprevmonth(month) {
if (month=="1")
return "12";
else
return (eval(month)-1);
}

function calcprevyear(month,year) {
if (month=="1")
return (eval(year)-1);
else
return (year);
}
Jul 23 '05 #1
7 1870
Fendi Baba wrote on 04 apr 2004 in comp.lang.javascript:
The function is called from opencalendar(targetfield). Thanks for any
hints on what could be the problem.


What error?
On what line?

Even better show us a working example URL.

btw: eval() is evil.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #2
ef*****@epitome.com.sg (Fendi Baba) writes:

Please don't make your post unreadable without the subject. The body
should be self contained.

[Can anyhelp me, what does this javascript calendar give error for
dates before year 2000?]
The function is called from opencalendar(targetfield). Thanks for any
hints on what could be the problem.
I have a pretty good idea, but the code is generally so lousy that I
would consider scrapping it and starting over. I am sure whatever it
is doing can be done shorter, smarter, and less error prone (taking
the numerous occurences of "eval" as a clear sign of an programmer
that doesn't know the language very well).
Anyway:
var yearA = new Array(); .... for(var i=0;i<10;i++)
{
yearA[i]=gy(i);
}
This fills an array with ten years.
function gy(num) {
var mydate = new Date();
return (eval(mydate.getYear()) - 4 + num);
}
For arguments between 0 and 9 (as it is called above), this function
returns values between 2000 and 2009 (generally, current year - 4 and
forward).
As a side note, the call to eval does nothing here, except transform
a number to a string and parse it as an expression to get the number
back ... huge overhead for nothing.

So, your table only contains years between 2000 and 2009. I guess you'll
get errors for dates after 2009 too.
function gm(num) {
var mydate = new Date();
mydate.setDate(1);
mydate.setMonth(num-1);
var datestr = "" + mydate;
return datestr.substring(4,7);


This line stood out as particulary fragile. You can not assume anything
about the format of the string representation of a date.
In my browser, the above function gives:
gm(4) == " 01"
That is the date set to 1, not the month.

Consider using another script. It'll be easier than patching up this one.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3
In article <c9**************************@posting.google.com >, Fendi
Baba <ef*****@epitome.com.sg> wrote:
The function is called from opencalendar(targetfield). Thanks for any
hints on what could be the problem.

<snip>

Double check all your single and double quote pairings. There may be a
problem someplace. I get an unterminated string constant error
someplace on your DOCUMENT WRITE INPUT statements.

BTW: All of your literal values should have quotes around them.
TYPE='xxx' NAME='xxx' etc.

Why don't you explain what you are trying to do with the calendar. What
are all the BUTTONS?

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #4
In article <04*************************@domain.invalid>, Dennis M.
Marks <de******@domain.invalid> wrote:
In article <c9**************************@posting.google.com >, Fendi
Baba <ef*****@epitome.com.sg> wrote:
The function is called from opencalendar(targetfield). Thanks for any
hints on what could be the problem.

<snip>

Double check all your single and double quote pairings. There may be a
problem someplace. I get an unterminated string constant error
someplace on your DOCUMENT WRITE INPUT statements.

BTW: All of your literal values should have quotes around them.
TYPE='xxx' NAME='xxx' etc.

Why don't you explain what you are trying to do with the calendar. What
are all the BUTTONS?


Ignore what I said about quotes. I finially got it to work with no
changes and it does years before 2000. The 1-9 buttons are larger than
the rest. I'm still not sure what the date buttons are supposed to do.
They just close the window.

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #5
Lasse

You're right. The problem with this code is the fact that it returns
year as undefined if the year is before 2000. You're probably right
that it could return error beyond 2009 as well.

This wasn't my code and I didn't realise this until a user pointed out
the problem.

I saw a few other javascript calendar and it was neat but I'm not need
to get his fixed by tomorrow.

Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<sm**********@hotpop.com>...
ef*****@epitome.com.sg (Fendi Baba) writes:

Please don't make your post unreadable without the subject. The body
should be self contained.

[Can anyhelp me, what does this javascript calendar give error for
dates before year 2000?]
The function is called from opencalendar(targetfield). Thanks for any
hints on what could be the problem.


I have a pretty good idea, but the code is generally so lousy that I
would consider scrapping it and starting over. I am sure whatever it
is doing can be done shorter, smarter, and less error prone (taking
the numerous occurences of "eval" as a clear sign of an programmer
that doesn't know the language very well).
Anyway:
var yearA = new Array();

...
for(var i=0;i<10;i++)
{
yearA[i]=gy(i);
}


This fills an array with ten years.
function gy(num) {
var mydate = new Date();
return (eval(mydate.getYear()) - 4 + num);
}


For arguments between 0 and 9 (as it is called above), this function
returns values between 2000 and 2009 (generally, current year - 4 and
forward).
As a side note, the call to eval does nothing here, except transform
a number to a string and parse it as an expression to get the number
back ... huge overhead for nothing.

So, your table only contains years between 2000 and 2009. I guess you'll
get errors for dates after 2009 too.
function gm(num) {
var mydate = new Date();
mydate.setDate(1);
mydate.setMonth(num-1);
var datestr = "" + mydate;
return datestr.substring(4,7);


This line stood out as particulary fragile. You can not assume anything
about the format of the string representation of a date.
In my browser, the above function gives:
gm(4) == " 01"
That is the date set to 1, not the month.

Consider using another script. It'll be easier than patching up this one.

/L

Jul 23 '05 #6
Lasse, Evertjan

Thank you for responding.

In particular Lasse's note has been pretty helpful. I recognise the
fact that the code left a lot to be desired. As I need to get it
working by tomorrow, I hv decided to stick with it first.

For the moment, I have increased the year array to 105 value thus
allowing the year to move back and forth 50 years. Tested and it
worked fine. I'll look through Lasse comment about the string format
and will try to rework this entire code. As it wasn''t something I
wrote initial, I am thankful that you've help me sort of figure out
some of the logics.

Thanks
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<sm**********@hotpop.com>...
ef*****@epitome.com.sg (Fendi Baba) writes:

Please don't make your post unreadable without the subject. The body
should be self contained.

[Can anyhelp me, what does this javascript calendar give error for
dates before year 2000?]
The function is called from opencalendar(targetfield). Thanks for any
hints on what could be the problem.


I have a pretty good idea, but the code is generally so lousy that I
would consider scrapping it and starting over. I am sure whatever it
is doing can be done shorter, smarter, and less error prone (taking
the numerous occurences of "eval" as a clear sign of an programmer
that doesn't know the language very well).
Anyway:
var yearA = new Array();

...
for(var i=0;i<10;i++)
{
yearA[i]=gy(i);
}


This fills an array with ten years.
function gy(num) {
var mydate = new Date();
return (eval(mydate.getYear()) - 4 + num);
}


For arguments between 0 and 9 (as it is called above), this function
returns values between 2000 and 2009 (generally, current year - 4 and
forward).
As a side note, the call to eval does nothing here, except transform
a number to a string and parse it as an expression to get the number
back ... huge overhead for nothing.

So, your table only contains years between 2000 and 2009. I guess you'll
get errors for dates after 2009 too.
function gm(num) {
var mydate = new Date();
mydate.setDate(1);
mydate.setMonth(num-1);
var datestr = "" + mydate;
return datestr.substring(4,7);


This line stood out as particulary fragile. You can not assume anything
about the format of the string representation of a date.
In my browser, the above function gives:
gm(4) == " 01"
That is the date set to 1, not the month.

Consider using another script. It'll be easier than patching up this one.

/L

Jul 23 '05 #7
JRS: In article <c9**************************@posting.google.com >, seen
in news:comp.lang.javascript, Fendi Baba <ef*****@epitome.com.sg> posted
at Sun, 4 Apr 2004 05:29:00 :
The function is called from opencalendar(targetfield). Thanks for any
hints on what could be the problem.


Pretty well all of it needs rewrite, to be correct and efficient.

Do not let your news system wrap code that you post; it is your duty to
ensure that posted code is runnable code.

If you *must* define a table of months, do it ONCE, globally; and,
rather than
var monthA = new Array();
monthA[0]="January";
monthA[1]="February"; ...
use
var monthA = ["January", "February", ... ]

If the data is well enough controlled to use parseInt with only one
parameter, you should be able to use unary + instead.

In one place, you have the full Gregorian leap year rule; in another,
you use the Julian.

function calclastday(month,year) {
if (month==2) return 28 + (year%4)==0) // 1901-2099
return [,31,0,31,30,31,30, 31,31,30,31,30,31][month] }

beats what you have; but using a Date Object is more direct.

Typing could be saved by

var df0 = document.forms[0]

and using that thereafter; and likewise elsewhere.
Read the newsgroup FAQ; see below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark A. Washburn | last post by:
The Julian Calendar, adopted in 46BC by Julius Caesar, adds one day every four years to correct for the fact that Earth's solar year is slightly more than 365 of Earth's daily rotations. ( 365.25...
10
by: vinu | last post by:
I have a javascript file named select.js. This is a file which is use to pop up a calendar, when clicked on a calendar icon in an ASP file. have a close button in the calendar. When i click on the...
4
by: ianv2 | last post by:
Hi Is the following possible using Javascript ? I would like a page to redirect to another page if the page expiry has passed. E.G. If my questionnaireform.html page had an expiry date...
7
by: Shuffs | last post by:
Could someone, anyone please tell me what I need to amend, to get this function to take Sunday as the first day of the week? I amended the Weekday parts to vbSunday (in my code, not the code...
5
by: Mal | last post by:
Hello. I have a database that tracks reservations at a campground. I want to be able to make a calendar type report that shows how many people are here in given period. Stored for each...
11
by: gan | last post by:
hi dear all please help me im using the calander control from a form just i paste here the problem, the problem is when i click the calander button the calander is opening at the same time...
1
by: KRISHNA PRAVI | last post by:
the error is "runtime error object expected" here is the code....................................................................................... <script language="javascript"...
7
by: William (Tamarside) | last post by:
Please, if you have the time and knowledge to help me I'd truly appreciate it! I need to build a calendar page that displays available/unavailable info from a DB and colour a cell according to...
1
by: xtremebass | last post by:
Hello Bytes, i have a calender program which is created by using Javascript. when i execute that program using Internet Explorer,it works properly but when i tried in Mozilla firefox it didnt...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.