Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with date 1

Status
Not open for further replies.

nalbiso

Programmer
Aug 31, 2000
71
0
0
US
Hello,

I need help with a date processing function. I have a form where my customers will put in a date (in format mm/dd/yy). But I want that date to be used to update another field on the form with just the day of the week only.

I've tried this:


<script type=&quot;text/javascript&quot;>
var d=new Date()
var weekday=new Array(&quot;Sunday&quot;,&quot;Monday&quot;,
&quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;,
&quot;Friday&quot;,&quot;Saturday&quot;)
</script>

And then calling the following the following during an onblur:

document.estform.startingdayofweek.value=weekday[d.getDay(document.estform.startdate.value)]


But this just keeps returning today's weekday in the startingdayofweek textbox.

Can someone please let me know what I am doing wrong here or if there is a more efficient way to do this, please let me know?

Thanks!
 
Is startdate a text field also? If it's a drop-down list, you'd want to reference it as:

Code:
document.estform.startingdayofweek.value = document.estform.startdate.options[document.estform.startdate.selectedIndex].value

...or, more neatly...

Code:
var e = document.estform;
var s = e.startdate;
e.startingdayofweek.value = s.options[s.selectedIndex].value;


Or... now that I look at it closer (and here is the real answer to your question... assuming you are NOT using select lists):

do NOT use d.getDay(document.estform.startdate.value). The getDay() function does not use parameters. It does not matter what you send as a parameter, it will always refer to the d (date) object which is today. Rather, do this:

Code:
var e = document.estform;
e.startingdayofweek.value=weekday[e.startdate.value];

This assumes that startdate is a text field and the value entered is an integer 0 (Sunday) through 6 (Saturday).

'hope this helped (if it didn't put you to sleep).

--Dave
 
Thanks for replying.

Actually startdate is the textbox where the customer puts in the date (in format mm/dd/yy).

There are two textboxes.

startdate and startingdayofweek, in which I would like the day of the week to appear depending on what date the customer puts in the startdate field.
 
Try:

Code:
var e = document.estform;
var day = e.startdate.value; // mm/dd/yyyy
day = day.substring(day.indexOf(&quot;/&quot;)+1); // dd/yyyy
day = day.substring(0, day.indexOf(&quot;/&quot;)); // dd
e.startingdayofweek.value = weekday[day];

See note in previous post above about use of getDay() function.

'hope that helps.

--Dave
 
<script type=&quot;text/javascript&quot;>
var weekday=[&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;]
</script>
<form name=&quot;estform&quot;>

<input type=&quot;text&quot; onblur=&quot;document.estform.startingdayofweek.value=weekday[new Date(this.value).getDay()]&quot;>

Make sure you validate that they enter a 4 digit year. I believe a 2 digit year like &quot;03&quot; will be read as &quot;1903&quot;.

Adam
 
That worked. Thanks for the help! [thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top