473,408 Members | 2,839 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,408 software developers and data experts.

begin date-end date -> move to 10row selectbox

Hello everybody,

I have a *big* problem. I thought its not that big problem for you
professionals...

Anyway, I have a begin date which has 3 dropdown boxes
(day/Month/Year).
The same for the end date.
So if somebody selects the begin date 13.12.2004 and the end date
19.12.2004
all the dates between should be moved through javascript to the 10 row
multiple selectbox. Those following shuold be moved:
13.12.2004
14.12.2004
15.12.2004
16.12.2004
17.12.2004
18.12.2004
19.12.2004
After I entered the begin date, I enter the end date, after that I
press on a link which moves the dates to the select box.
I can not make any reloads - it has to be made through javascript by
clicking a link e.g. - thats why I am in that newsgroup ;-)

I hope I described the problem good enough - thank you for your
sympathies

Dieter
Jul 20 '05 #1
6 2193
Ki**********@yahoo.de (Piotr Pietrowski) writes:
Anyway, I have a begin date which has 3 dropdown boxes
(day/Month/Year).
The same for the end date.
Ok. You should test for illegal inputs too (31th of February and the like).
But let's convert them to dates:

function convertToDate(day,month,year) {
day = Number(day);
month = Number(month)-1;
year = Number(year);
var date = new Date(year,month,day);
if (month != date.getMonth() || day != date.getDate()) {
return null; // or throw an error.
}
return date;
}

You can then find the start and end date (the varable "form" is
assumed to refer to the form the select elements are in):
---
var elems = form.elements;
var fromSelectDay = elem['fromSelectDay'];
var fromSelectMonth = elem['fromSelectMonth'];
var fromSelectYear = elem['fromSelectYear'];
var fromDate = convertToDate(
fromSelectDay.options[fromSelectDay.selectedIndex].value,
fromSelectMonth.options[fromSelectMonth.selectedIndex].value,
fromSelectYear.options[fromSelectYear.selectedIndex].value);
if (fromDate == null) {
alert("From date illegal!");
// ... abort somehow
}

var toSelectDay = elem['toSelectDay'];
var toSelectMonth = elem['toSelectMonth'];
var toSelectYear = elem['toSelectYear'];
var toDate = convertToDate(
toSelectDay.options[toSelectDay.selectedIndex].value,
toSelectMonth.options[toSelectMonth.selectedIndex].value,
toSelectYear.options[toSelectYear.selectedIndex].value);
if (toDate == null) {
alert("To date illegal!");
// ... abort somehow
}
--- So if somebody selects the begin date 13.12.2004 and the end date
19.12.2004
all the dates between should be moved through javascript to the 10 row
multiple selectbox. [both inclusive]
After I entered the begin date, I enter the end date, after that I
press on a link which moves the dates to the select box.


Why a link. It would be more appropriate to have a button. Links link
to pages. Buttons activets functionalty.

Anyway, this code creates the ten new options and adds them to the
select called "targetSelect" of the same form as above:
---
function LZ(n) {// add leading zero
return (n<10?"0":"")+n;
}

var targetSelect = elems['targetSelect'];
targetSelect.options.length = 0; // clear previous content
while(fromDate <= toDate) {
var year = fromDate.getYear(); // use getFullYear if you
// only target modern browsers
if (year < 1000) {year += 1900;} // and then you can omit this
var month = fromDate.getMonth()+1;
var day = formDate.getDate();
var dateString = LZ(day)+"."+LZ(month)+"."+year;
targetSelect.options[targetSelect.options.length] =
new Option(dateString,dateString);

fromDate.setDate(fromDate.getDate()+1); // next day
}
---

That should do it, if you execute all of this when you click on the
button.

/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 20 '05 #2
"Piotr Pietrowski" <Ki**********@yahoo.de> wrote in message
news:12**************************@posting.google.c om...
Hello everybody,

I have a *big* problem. I thought its not that big problem for you
professionals...

Anyway, I have a begin date which has 3 dropdown boxes
(day/Month/Year).
The same for the end date.
So if somebody selects the begin date 13.12.2004 and the end date
19.12.2004
all the dates between should be moved through javascript to the 10 row
multiple selectbox. Those following shuold be moved:
13.12.2004
14.12.2004
15.12.2004
16.12.2004
17.12.2004
18.12.2004
19.12.2004
After I entered the begin date, I enter the end date, after that I
press on a link which moves the dates to the select box.
I can not make any reloads - it has to be made through javascript by
clicking a link e.g. - thats why I am in that newsgroup ;-)

I hope I described the problem good enough - thank you for your
sympathies

Dieter


Here's an ugly solution that populates ten SELECT options with the starrting
date and the next nine days: Watch for word-wrap.

<html>
<head>
<title>DDMMYY.htm</title>
<script type="text/javascript">
function populate(former) {
var DD = former.DD.options[former.DD.selectedIndex].text;
var MM = former.MM.options[former.MM.selectedIndex].text;
var YY = former.YY.options[former.YY.selectedIndex].text;
var DA = parseInt(DD);
var MO = parseInt(MM);
var YR = parseInt(YY);
var LY = false;
if (YR % 4 == 0 && (YR % 100 != 0 || YR % 400 == 0)) LY = true;
if (MO == 4 || MO == 6 || MO == 9 || MO == 11) {
if (DA == 31) DA = 0;
} else if (MO == 2) {
if (DA == 29 && !LY) DA = 0;
if (DA == 30 || DA == 31) DA = 0;
}
if (DA == 0) {
alert("Invalid Day for this Month!");
return;
}
var ZZ = new Array();
ZZ[0] = DD + "/" + MM + "/" + YY;
for (var i=1; i<former.ZZ.length; i++) {
DA++;
if (DA > 31) {
DA = 0;
if (MO == 12) {
MO = 0;
YR++;
}
} else if (MO == 4 || MO == 6 || MO == 9 || MO == 11) {
if (DA > 30) DA = 0;
} else if (MO == 2) {
if (LY) {
if (DA > 29) DA = 0;
} else {
if (DA > 28) DA = 0;
}
}
if (DA == 0) {
DA++;
MO++;
}
DD = (DA + 100) + "";
MM = (MO + 100) + "";
ZZ[i] = DD.substr(1,2) + "/" + MM.substr(1,2) + "/" + YR;
}
for (var j=0; j<ZZ.length; j++) {
former.ZZ.options[j].text = ZZ[j];
}
}
</script>
</head>
<body>
<form>
<select name="DD">
<option>01<option>02<option>03<option>04<option> 05
<option>06<option>07<option>08<option>09<option> 10
<option>11<option>12<option>13<option>14<option> 15
<option>16<option>17<option>18<option>19<option> 20
<option>21<option>22<option>23<option>24<option> 25
<option>26<option>27<option>28<option>29<option> 30
<option>31
</select>
<select name="MM">
<option>01<option>02<option>03<option>04<option> 05
<option>06<option>07<option>08<option>09<option> 10
<option>11<option>12
</select>
<select name="YY">
<option>2003<option>2004
</select>
<input type="button" value="OK" onclick="populate(this.form)">
<br><br>
<select name="ZZ" size="10">
<option><option><option><option><option>
<option><option><option><option><option>
</select>
</form>
</body>
</html>
Jul 20 '05 #3
JRS: In article <FDfQb.109361$nt4.410819@attbi_s51>, seen in
news:comp.lang.javascript, McKirahan <Ne**@McKirahan.com> posted at Fri,
23 Jan 2004 20:39:01 :-

Here's an ugly solution that populates ten SELECT options with the starrting
date and the next nine days: Watch for word-wrap.

Trash.
function populate(former) {
var DD = former.DD.options[former.DD.selectedIndex].text;
var MM = former.MM.options[former.MM.selectedIndex].text;
var YY = former.YY.options[former.YY.selectedIndex].text;
var DA = parseInt(DD);
var MO = parseInt(MM);
ISTM that you have designed to allow failure during the first nine
months of the year and the first nine days of the remaining months.

parseInt("08") = parseInt("09") = 0

var DA = + former.DD.options[former.DD.selectedIndex].text;

var YR = parseInt(YY); var LY = false;
if (YR % 4 == 0 && (YR % 100 != 0 || YR % 400 == 0)) LY = true;
var LY = (YR % 4 == 0 && (YR % 100 != 0 || YR % 400 == 0)) ;

is better than the two lines quoted, although not needed.
if (MO == 4 || MO == 6 || MO == 9 || MO == 11) {
if (DA == 31) DA = 0;
} else if (MO == 2) {
if (DA == 29 && !LY) DA = 0;
if (DA == 30 || DA == 31) DA = 0;
}
if (DA == 0) {
alert("Invalid Day for this Month!");
return;
Unnecessary if a Date Object is used; see LRN's post.
}
var ZZ = new Array();
ZZ[0] = DD + "/" + MM + "/" + YY;
for (var i=1; i<former.ZZ.length; i++) {
DA++;
if (DA > 31) {
DA = 0;
if (MO == 12) {
MO = 0;
YR++;
}
} else if (MO == 4 || MO == 6 || MO == 9 || MO == 11) {
if (DA > 30) DA = 0;
} else if (MO == 2) {
if (LY) {
if (DA > 29) DA = 0;
} else {
if (DA > 28) DA = 0;
}
}
if (DA == 0) {
DA++;
MO++;
}
Unnecessary if a Date Object is used; see LRN's post.
DD = (DA + 100) + "";
MM = (MO + 100) + "";
ZZ[i] = DD.substr(1,2) + "/" + MM.substr(1,2) + "/" + YR;
The OP did ask for dots, not slashes.

Where code is repetitive, a function should be used.
}
for (var j=0; j<ZZ.length; j++) {
former.ZZ.options[j].text = ZZ[j];
}
}

Reading on a topic should precede writing answers on it.
Piotr : Unless you want to confuse the Americans (often amusing), I
suggest that you use YYYY-MM-DD for dates. Anyone can understand that
unambiguous format.

--
© 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 20 '05 #4
"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:XC**************@merlyn.demon.co.uk...
JRS: In article <FDfQb.109361$nt4.410819@attbi_s51>, seen in
news:comp.lang.javascript, McKirahan <Ne**@McKirahan.com> posted at Fri,
23 Jan 2004 20:39:01 :-

Here's an ugly solution that populates ten SELECT options with the starrtingdate and the next nine days: Watch for word-wrap.

Trash.

I did say it was ugly!

I didn't realize that it was incorrect; nor how inefficient it was.

How about:
<html>
<head>
<title>DDMMYYLZ.htm</title>
<script type="text/javascript">
function LZ(n) {
// add leading zero
return (n<10?"0":"")+n;
}
function OK(former) {
var DA = parseInt(former.DD.options[former.DD.selectedIndex].value);
var MO = parseInt(former.MM.options[former.MM.selectedIndex].value);
var YR = parseInt(former.YY.options[former.YY.selectedIndex].value);
var DT = new Date(YR,MO-1,DA);
if (DT == null) {
alert("Invalid Date");
return;
}
for (var i=0; i<former.ZZ.length; i++) {
var YY = DT.getFullYear();
var MM = DT.getMonth()+1;
var DD = DT.getDate();
former.ZZ.options[i].text = LZ(DD) + "." + LZ(MM) + "." + LZ(YY);
DT.setDate(DT.getDate()+1);
}
}
</script>
</head>
<body>
<form>
<select name="DD">
<option value="1">01<option value="2">02<option value="3">03
<option value="4">04<option value="5">05<option value="6">06
<option value="7">07<option value="8">08<option value="9">09
<option value="10">10<option value="11">11<option value="12">12
<option value="13">13<option value="14">14<option value="15">15
<option value="16">16<option value="17">17<option value="18">18
<option value="19">19<option value="20">20<option value="21">21
<option value="22">22<option value="23">23<option value="24">24
<option value="25">25<option value="26">26<option value="27">27
<option value="28">28<option value="29">29<option value="30">30
<option value="31">31
</select>
<select name="MM">
<option value="1">01<option value="2">02<option value="3">03
<option value="4">04<option value="4">05<option value="6">06
<option value="7">07<option value="8">08<option value="9">09
<option value="10">10<option value="11">11<option value="12">12
</select>
<select name="YY">
<option value="2003">2003<option value="2004">2004
</select>
<input type="button" value="OK" onclick="OK(this.form)">
<br><br>
<select name="ZZ" size="10">
<option><option><option><option><option>
<option><option><option><option><option>
</select>
</form>
</body>
</html>
Note: The OP used a Begin Date and an End Date to fill in a drop-down list
of ten items. This approach lets the "size" of the select determine who
many "options" to generate based only on a Begin Date. Perhaps the OP
wanted a drop=down list of all dates between Begin Date and End Date but it
wasn't stated so I interpreted his requirement to always show ten dates.
Jul 20 '05 #5
"McKirahan" <Ne**@McKirahan.com> writes:
How about: function OK(former) {
var DA = parseInt(former.DD.options[former.DD.selectedIndex].value);
You still use parseInt without the second argument. You have added values
to all the options without prefixed zeros, which avoids the problem, but
it would have been *much* easier to just add a ",10" to tell parseInt
to use decimal, or use the Number function instead of parseInt.
var DT = new Date(YR,MO-1,DA);
if (DT == null) {
alert("Invalid Date");
Using "new Date" will *never* return null. Even if all three are NaN, it
will return a Date object (although with its valueOf returns NaN).
The test for whether DT is not the correct date is, e.g.:

if (DT.getMonth() != MO-1 || DT.getDate() != DA) {
// invalid date
}
for (var i=0; i<former.ZZ.length; i++) { .... former.ZZ.options[i].text = LZ(DD) + "." + LZ(MM) + "." + LZ(YY);


You migth run into troubles in some browers here, because the text
property of an option is described as read-only in the DOM
specification. I recommend createing a new Option instead:

var dateString = LZ(DD) + "." + LZ(MM) + "." + YY;
former.elements['ZZ'].options[i] = new Option(dateString);

/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 20 '05 #6
JRS: In article <fz**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Sun, 25 Jan 2004 23:25:52 :-

Using "new Date" will *never* return null. Even if all three are NaN, it
will return a Date object (although with its valueOf returns NaN).
The test for whether DT is not the correct date is, e.g.:

if (DT.getMonth() != MO-1 || DT.getDate() != DA) {
// invalid date
}


Since the values come from select controls, the only possible error is
that DA is in 29,30,31 and too big for its MO. Therefore, each one of
the two tests is sufficient on its own, in this case.

My js-date6.htm has a selector where the days offered depend on month
and year chosen, making invalidity impossible. It does make the code
longer.

Unary + could be used instead of parseInt.

ISTM that, in options, there is nothing special about value, and another
name could be used; and that, instead of setting value in HTML, it could
be set in javascript. Setting it to 0123, it reads back as 83, which
proves numericality. But that's probably more interesting than useful.

--
© 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 20 '05 #7

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

Similar topics

2
by: Werner Partner | last post by:
I'm a little confused: htmled phase5 uses this Date-Tag: <!--DATE-->28.03.2004<!--/DATE--> If I try to validate the source, sometimes I get an error, sometimes not. This source:...
5
by: Martien van Wanrooij | last post by:
I would like to retrieve, let us say, the First Monday after a certain date, so my (imaginary) function could be something like echo weekdayAfter("28 July 2005", "Monday") should return "1 August...
2
by: David Fickbohm | last post by:
People, I am trying to determine the creation date of files in a folder. I am using the following code to find the folder and confirm that files exist in the folder. If someone could give me an...
1
by: Paul King | last post by:
Hi there. I want to construct a SQL ASPX page and wish to fetch all information back. At the moment I have the following script: Dim strSqlProducts As String = "SELECT...
39
by: eruanion | last post by:
Hi, I've been working on this for a while now and I can't seem to find out what is wrong with my code. I have 2 files one c3common.js which only contains javascript functions for my main html page...
1
by: nickdahat | last post by:
OK, here's the quirk I have. I'm building a scheduler for a Fire Truck Manufacturing company. They have like 8 date fields per truck to be built. If they have to squeeze in a truck between...
1
by: sumitshining | last post by:
what would be the code for the date & time be updated automatically with the windowsas soon as a library management software is made to run?????????
0
by: gpspocket | last post by:
help me -CURSOR backward insert from End Date > to Start Date how to insert dates from end to start like this SELECT 111111,1,CONVERT(DATETIME, '17/03/2008', 103), CONVERT(DATETIME,...
0
by: kouliscon | last post by:
Hello All I have the following vbscript and i need help to modified in order to meet my needs since i dont know how to do it. the script is: -----------------------------------BEGIN OF...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.