473,748 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combo with dates

Hi,

I have a code for filling a dropdown, but I cannot work out how to make
it fill with every date, including todays date, as required below

i.e.
05/10/2006
06/10/2006
07/10/2006
08/10/2006
09/10/2006
10/10/2006
11/10/2006
12/10/2006
13/10/2006
14/10/2006
It needs to display about 10 dates

------------------------------------

The code I have is:

<script type="text/javascript" language="javas cript">
function formatDate(t_da te, format)
{
/* t_date is expected to be a Date() and format is a string */

var r_date = null; /* string to return */

/* add more formats as desired */
switch (format)
{
case ("mm/dd/yyyy"):
tmp = t_date.getDate( );
tmp = tmp<10?"0"+tmp: tmp;
r_date = tmp+"/";
tmp = t_date.getMonth ()+1;
tmp = tmp<10?"0"+tmp: tmp;

r_date += tmp+"/"+t_date.getFul lYear();
break;
default:
alert("That is not a valid format");
}
return r_date;
}

function addDays(t_date, days)
{
return new Date(t_date.get Time() + days*24*60*60*1 000);
}

function fillIt()
{
var s = document.forms["formname"].elements["Date"];
var o;
var theDate = new Date();
var i;

for (i=0; i<20; i++)
{
/* if this is a Tuesday (2) or Thursday (4), put in option, else
add a
day and check again */
while (theDate.getDay () != 0 )
theDate = addDays(theDate , 1);
/* we get here, it's either Tursday or Thursday - write option,
increment, and loop */
tmp = formatDate(theD ate, "mm/dd/yyyy");
o = new Option(tmp, tmp);
s.options[i] = o;
theDate = addDays(theDate ,1);
}
}
</script>
-----------------------------------
Appreciate your help, thanks :-)

David

Oct 5 '06 #1
3 1533
David wrote on 05 okt 2006 in comp.lang.javas cript:
Hi,

I have a code for filling a dropdown, but I cannot work out how to make
it fill with every date, including todays date, as required below

i.e.
05/10/2006
06/10/2006
07/10/2006
08/10/2006
09/10/2006
10/10/2006
11/10/2006
12/10/2006
13/10/2006
14/10/2006
It needs to display about 10 dates
=============== =============== =============== ====
<script type='text/javascript'>

function nextday(x){
return new Date(x.getYear( ), x.getMonth(), x.getDate()+1);
};

function DDMMYYYY(x){
return T(x.getDate())+ '/'+T(x.getMonth( )+1)+'/'+x.getYear();
};

function T(x){
return (x<10)?'0'+x:x;
};

var d = new Date();
var t = '';

for (var i=0;i<10;i++) {
t += '<option>'+DDMM YYYY(d)+'</option>';
d = nextday(d);
}

document.write( '<SELECT>'+t+' </SELECT>');

</script>
=============== =============== =========

if you want only Tuesdays (2) or Thursdays (4):

=============== =============== =========

......
for (var i=0;i<10;i++) {
while (d.getDay() != 2 && d.getDay() != 4 )
d = nextday(d);
t += '<option>'+DDMM YYYY(d)+'</option>';
d = nextday(d);
}
.....

=============== =============== =========

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 5 '06 #2
David wrote:
Hi,

I have a code for filling a dropdown, but I cannot work out how to make
it fill with every date, including todays date, as required below

i.e.
05/10/2006
06/10/2006
07/10/2006
08/10/2006
09/10/2006
10/10/2006
11/10/2006
12/10/2006
13/10/2006
14/10/2006
Your codes seems to want only Tuesdays and Thursdays rather than every
day.

It needs to display about 10 dates

------------------------------------

The code I have is:

<script type="text/javascript" language="javas cript">
The language attribute is deprecated so remove it, keep type.

function formatDate(t_da te, format)
{
/* t_date is expected to be a Date() and format is a string */
If you want t_date to be today, then start with:

var t_date = new Date();
var r_date = null; /* string to return */
Initialising r_date with the value null serves no useful purpose in
this instance.

/* add more formats as desired */
switch (format)
{
case ("mm/dd/yyyy"):
tmp = t_date.getDate( );
tmp = tmp<10?"0"+tmp: tmp;
It is probably better to have a small function that adds the leading
zero rather than repeating the code, say:

function addZ(num){ return (num < 10)? '0' + num : '' + num; }

Then:

tmp = addZ(t_date.get Date());
r_date = tmp+"/";

tmp = t_date.getMonth ()+1;
tmp = tmp<10?"0"+tmp: tmp;

r_date += tmp+"/"+t_date.getFul lYear();
break;
Instead of all that, why not:

tmp = addZ(t_date.get Date()) + '/'
+ addZ(t_date.get Month()+1) + '/'
+ t_date.getFullY ear();

default:
alert("That is not a valid format");
}
return r_date;
}

function addDays(t_date, days)
{
return new Date(t_date.get Time() + days*24*60*60*1 000);
Adding 24hours worth of milliseconds is risky in places where daylight
saving is in effect, you will skip a day when going into daylight
saving and count a day twice on the way out. Just add the required
number of days to the date and avoid the issue:

return t_date.setDate( t_date.getDate( ) + (+days));

The brackets around +days is not essential but may help maintenance.
Note that adding days beyond the end of the month is OK: 30-Jan + 3
days gives 02-Feb.
Try this version[1] as an example:

function addDates(f){
var numOptions = 10;
var d = new Date(f.startDat e.value);
var sel = f.date;
sel.length = 0; // Removes existing options
var dateString, dayNum;

while (numOptions) {
dayNum = d.getDay();
if (2 == dayNum || 4 == dayNum){
dateString = addZ(d.getDate( )) + '/'
+ addZ(d.getMonth () + 1) + '/'
+ d.getFullYear() ;
sel.options[sel.options.len gth] = new Option(dateStri ng);
--numOptions;
}
d.setDate(d.get Date() + 1);
}
}

function addZ(num){
return (num < 10)? '0' + num : '' + num;
}

</script>

<form action="">
<input type="text" name="startDate " value="2006/09/25">
<select name="date">
<option>Click "Add dates" to add dates
</select>
<input type="button" value="Add dates"
onclick="addDat es(this.form)">
</form>
1. That the input date should be validated before trying to use it,
this version returns only Tuesdays and Thursdays in the option list
--
Rob

Oct 5 '06 #3

RobG wrote:
David wrote:
Hi,

I have a code for filling a dropdown, but I cannot work out how to make
it fill with every date, including todays date, as required below

i.e.
05/10/2006
06/10/2006
07/10/2006
08/10/2006
09/10/2006
10/10/2006
11/10/2006
12/10/2006
13/10/2006
14/10/2006

Your codes seems to want only Tuesdays and Thursdays rather than every
day.

It needs to display about 10 dates

------------------------------------

The code I have is:

<script type="text/javascript" language="javas cript">

The language attribute is deprecated so remove it, keep type.

function formatDate(t_da te, format)
{
/* t_date is expected to be a Date() and format is a string */

If you want t_date to be today, then start with:

var t_date = new Date();
var r_date = null; /* string to return */

Initialising r_date with the value null serves no useful purpose in
this instance.

/* add more formats as desired */
switch (format)
{
case ("mm/dd/yyyy"):
tmp = t_date.getDate( );
tmp = tmp<10?"0"+tmp: tmp;

It is probably better to have a small function that adds the leading
zero rather than repeating the code, say:

function addZ(num){ return (num < 10)? '0' + num : '' + num; }

Then:

tmp = addZ(t_date.get Date());
r_date = tmp+"/";

tmp = t_date.getMonth ()+1;
tmp = tmp<10?"0"+tmp: tmp;

r_date += tmp+"/"+t_date.getFul lYear();
break;

Instead of all that, why not:

tmp = addZ(t_date.get Date()) + '/'
+ addZ(t_date.get Month()+1) + '/'
+ t_date.getFullY ear();

default:
alert("That is not a valid format");
}
return r_date;
}

function addDays(t_date, days)
{
return new Date(t_date.get Time() + days*24*60*60*1 000);

Adding 24hours worth of milliseconds is risky in places where daylight
saving is in effect, you will skip a day when going into daylight
saving and count a day twice on the way out. Just add the required
number of days to the date and avoid the issue:

return t_date.setDate( t_date.getDate( ) + (+days));

The brackets around +days is not essential but may help maintenance.
Note that adding days beyond the end of the month is OK: 30-Jan + 3
days gives 02-Feb.
Try this version[1] as an example:

function addDates(f){
var numOptions = 10;
var d = new Date(f.startDat e.value);
var sel = f.date;
sel.length = 0; // Removes existing options
var dateString, dayNum;

while (numOptions) {
dayNum = d.getDay();
if (2 == dayNum || 4 == dayNum){
dateString = addZ(d.getDate( )) + '/'
+ addZ(d.getMonth () + 1) + '/'
+ d.getFullYear() ;
sel.options[sel.options.len gth] = new Option(dateStri ng);
--numOptions;
}
d.setDate(d.get Date() + 1);
}
}

function addZ(num){
return (num < 10)? '0' + num : '' + num;
}

</script>

<form action="">
<input type="text" name="startDate " value="2006/09/25">
<select name="date">
<option>Click "Add dates" to add dates
</select>
<input type="button" value="Add dates"
onclick="addDat es(this.form)">
</form>
1. That the input date should be validated before trying to use it,
this version returns only Tuesdays and Thursdays in the option list
--
Rob
----------------------

Thanks Rob,

How do I return all days including today ?

David

Oct 5 '06 #4

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

Similar topics

3
11070
by: pelcovits | last post by:
I am trying to set up an unbound form to enter report criteria. I've followed the MS Office Assistance document: "Create a form to enter report criteria" which describes how to enter data (such as dates) in a text box. This procedure works fine. However, I also need to enter data from a combo box and I cannot get this to work. On my form I've created an unbound combo box named "Name". In the query bound to the report I've entered the...
4
7192
by: meganrobertson22 | last post by:
Hi Everyone- I have a question about how to add and then use the "All" selection in a combo box. I am trying to figure out how to: (1) add "All" as a selection to a combo box and then (2) how to use the selection "All" as criteria for a field in a query, which is used to generate data for a report.
1
2189
by: keri | last post by:
I would like to have a combo box on a form that shows the results of a query, however the query is variable and i am unsure how to do this. I have NO knowledge of code so very basc instructions would be appreciated! I have a table "appointments" which contains an "appointment date" field. I have a form "planning" (based on another table) with a field for "date to plan". I would like a combo box to show any "appointments" already made...
2
2674
by: MobiusDick | last post by:
Hi All, I'm in the process of creating a database that allows users to enter details into a form which includes a date range as start and end dates. I have used the inbuilt Calendar Control 10.0 control to allow users to select the date. It is linked to two combo boxes and using the following code (this code is repeated for each combo box): Private Sub cmbStartDate_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As...
4
2170
by: nistala | last post by:
Hi, I have a combo box which has status(daily, monthly, weekly) enabled and a combo hidden.on change,ie., If i try to select weekly in the combo, it should enable the hidden combo with all the weekdays, and similarly if I try to select on monthly, the hidden combo should show, the dates from 1 to 30. Can you help me out in solving this issue?
10
1693
AllusiveKitten
by: AllusiveKitten | last post by:
Hi I am hoping someone can help me, I have a form with two combo boxes in it.... The first combo box gets its values from a table via an SQL query in the combo box properties. The second combo box is supposed to get its values also via an SQL query in its properties but it is depending on the result of the first combo box... ie. First combox has the names of all of the people who have received a document. (This combo box works perfectly...
2
3940
by: cmartin1986 | last post by:
First of all I want to thank all of you that have helped me in the past this is an awesome fourm. My problem today is I have a database that builds charts that are viewed by a large group every friday morning. I am building six different charts that all have query criteria of between two dates right now I am going and updating the charts manually every week. The dates these charts work on is always friday to thursday every week. Is there...
7
10693
by: clarencelai | last post by:
Hi.. I need to run some monthly reports that requires the user to pick a date (MMM-YYYY) from a drop down list. How can I list the dates (MMM-YYYY) in the combo box such that it would automatically include the current month, the last three months and the next three months? For example, the current month is Oct 2007, the combo list should list Jul 2007, Aug 2007, Sep 2007, Oct 2007, Nov 2007, Dec 2007 and Jan 2008. Cheers! Clarence
4
27802
by: novoselent | last post by:
This seems like it should be an easy thing, but I think I'm missing something simple here...or I'm just going about it all wrong... Using Access 2003 I have a form that lists vehicle service dates for different company terminals. The form loads showing all records. In the form header I have a combo box named "CSCFilter" that is unbound, and uses the table/query option to list the 16 terminals in our company. The field I'm trying to...
8
1831
by: troy_lee | last post by:
I want to look at the values of six combo boxes. If any of the boxes are not null, I want to change the value of a separate text box. How can I look at all of them at once? Thanks in advance. Troy Lee
0
8994
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8247
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.